[Solved] creating macros for locking range of cell on click of checkbox [closed]


Go to the worksheet of interest and:

1) Click the top left to grey triangle to select all cells

2) Right click Format Cells

3) Ensure Locked box is ticked

4) Go to Review tab and select Protect Sheet and enter your password e.g. “password”

In the code associated with the check box you would have something along the lines of the following. It should give you the general elements.

Private Sub CheckBox1_Click()
Dim wb as Workbook
Dim ws as Worksheet
Dim myRange as Range
Dim response As String

Set wb = ThisWorkbook
Set ws = wb.Sheets("mySheetName") 'Change as appropriate
Set myRange = ws.Range("A1:A2") 'Change as appropriate

If CheckBox1.Value = True Then 
    'unlock range  
   response = InputBox("Enter Password")
   If response <> "password" Then 
       Exit Sub 'Or other response such as loop to prompt again
   Else
       ws.Unprotect Password:="password"
       myRange.Locked = False
   End If
Else 
   'range should be locked
    ws.Unprotect Password:="password"
    myRange.Locked = True
End If

ws.Protect Password:="password"

End Sub

You would need to lock the VBA project so people can’t go into the VBE and retrieve the password.

Some info here on locking:

VBA Unlock Range

2

solved creating macros for locking range of cell on click of checkbox [closed]