VBA checkbox lock row

kennethshuen

New Member
Joined
Apr 2, 2019
Messages
1
Hi guys,

I would like to make a code in VBA that locks the entirerow whenever the checkbox is checked on both sheet 1 (starting from row 3), sheet 2 (starting from row 4). What I did now was using the code below on sheet 2, and using formular [=IF('sheet 2 defined cell value <>"", "1","")] on sheet 1 to try to prompt the same code with another private sub. But apparently that doesn't work as planned and the code below isn't perfect as well. Please comment below for any ideas!!! Thank you guys! Feel free to raise any queries and I will be happy to further elaborate on this subject.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim a As Long


If Not Intersect(Target, Columns("P")) Is Nothing Then
' the changed cell is in column P
If Target.Cells.Count = 1 Then
' only act if it is a single cell, so multicell copy will not trigger this
If Target.Value <> "" Then
Me.Unprotect Password:="123"
Target.EntireRow.Locked = True
Me.Protect Password:="123"
Else
Me.Unprotect Password:="123"
End If
End If
End If
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
But what do you need to do? If you write a data in column P, then locks the row?

Start with this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    If Not Intersect(Target, Columns("P")) Is Nothing Then
    ' the changed cell is in column P
        If Target.Row > 3 Then
            If Target.Cells.Count = 1 Then
                ' only act if it is a single cell, so multicell copy will not trigger this
                If Target.Value <> "" Then
                    Me.Unprotect Password:="123"
                    Target.EntireRow.Locked = True
                    Me.Protect Password:="123"
                End If
            End If
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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