Lock cell

Ciccio86

New Member
Joined
Feb 3, 2023
Messages
27
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm trying to lock a range when I tick a checkbox.
Ex. If I tick the checkbox in C1 the range A1:B1 get locked
The sheet is already protected but I have editable ranges

But every time I tick the checkbox all the editable ranges get locked

Cattura.PNG


VBA Code:
Private Sub CheckBox1_Click()

    Dim chk As OLEObject

    Set chk = Me.OLEObjects(CheckBox1.Name)

    Call Lock(chk)

End Sub
VBA Code:
Sub Lock(chk As OLEObject)
Dim r As Long
    r = chk.TopLeftCell.Row
    ActiveSheet.Unprotect Password:="Secret"
    Range("A" & r).Locked = True
    ActiveSheet.Protect Password:="Secret"
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I'm going to give you two solutions. The one you wanted with the checkboxes and my version with Wingding Characters in cells that get altered with a DoubleClick macro. I like using the Wingding checkboxes because I can copy them down to as many cells as I want. I named the range "CheckBoxes". The macro looks at that range to see if that is one of the cells you doubleclicked.


Book3
ABCD
1Job StartJob EndStatusStatus2
2þ
3¨
4¨
Sheet1


1677621817663.png


VBA Code:
Private Sub CheckBox1_Click()
  Dim chk As OLEObject

    Set chk = Me.OLEObjects(CheckBox1.Name)
    
    Call ToggleLock(chk.TopLeftCell.Row, Me.CheckBox1.Value)
    
End Sub



Sub ToggleLock(ChkRow, ChkStatus As Boolean)
    ActiveSheet.Unprotect Password:="Secret"
    If ChkStatus = True Then
      Range("A" & ChkRow & ":B" & ChkRow).Locked = True
    Else
      Range("A" & ChkRow & ":B" & ChkRow).Locked = False
    End If
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingCells:=True, Password:="Secret"
End Sub



Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  Dim i As Range
  Set i = Intersect(Target, Range("Checkboxes"))
  If Not i Is Nothing Then
    Cancel = True
    ActiveSheet.Unprotect Password:="Secret"
    Application.EnableEvents = False
    If i.Value = "þ" Then
      i.Value = "¨"
      ToggleLock i.Row, False
    Else
      i.Value = "þ"
      ToggleLock i.Row, True
    End If
    Application.EnableEvents = True
    
  End If
    
End Sub
 
Upvote 0
Solution
You know how to create a named range right? The range with the Wingding characters needs to be unprotected.
 
Upvote 0
I'm going to give you two solutions. The one you wanted with the checkboxes and my version with Wingding Characters in cells that get altered with a DoubleClick macro. I like using the Wingding checkboxes because I can copy them down to as many cells as I want. I named the range "CheckBoxes". The macro looks at that range to see if that is one of the cells you doubleclicked.


Book3
ABCD
1Job StartJob EndStatusStatus2
2þ
3¨
4¨
Sheet1


View attachment 86456

VBA Code:
Private Sub CheckBox1_Click()
  Dim chk As OLEObject

    Set chk = Me.OLEObjects(CheckBox1.Name)
 
    Call ToggleLock(chk.TopLeftCell.Row, Me.CheckBox1.Value)
 
End Sub



Sub ToggleLock(ChkRow, ChkStatus As Boolean)
    ActiveSheet.Unprotect Password:="Secret"
    If ChkStatus = True Then
      Range("A" & ChkRow & ":B" & ChkRow).Locked = True
    Else
      Range("A" & ChkRow & ":B" & ChkRow).Locked = False
    End If
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingCells:=True, Password:="Secret"
End Sub
Thanks For the fast reply.
With this code when i tick a checkbox all the sheet get locked and not only the checkbox row.

Edit: I have to update the status first time, is possible to update on workbook open?
 
Last edited:
Upvote 0
You need to unlock the cells that the check boxes are in

It worked for me
 
Upvote 0

Forum statistics

Threads
1,214,427
Messages
6,119,419
Members
448,895
Latest member
omarahmed1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top