Lock Sheet Row Based on Value in Column 8

tomexcel1

New Member
Joined
Feb 22, 2018
Messages
47
Hi All

I currently have the below code as a worksheet change, however i now need it to run when a button is pressed. Can someone please help me convert it to work like that, ive tired but i keep getting an object error.

Code:
[COLOR=#333333]Private Sub Worksheet_Change(ByVal Target As Range)[/COLOR]    Sheet17.Unprotect ("password")
    Dim cl As Range
    If Target.Column = 8 Then
        For Each cl In Target.Cells
            If UCase(cl.Value) = UCase("Authorised") And cl.Column = 8 Then
                Range("a" & cl.Row & ":h" & cl.Row).Locked = True
            Else
                Range("a" & cl.Row & ":h" & cl.Row).Locked = False
            End If
        Next
    End If
    Sheet17.Protect ("password")[COLOR=#333333]End Sub[/COLOR]

Thanks in advance
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hello,

You just need to add the code to it's own Sub then call the sub from the click event of the button (slightly different dependant on Form control/active x control)

For example:

Code:
Private Sub LockRow()
    
    'handle any errors
    On Error GoTo errHandle
    
    'ensure only one cell is active
    If Selection.Cells.Count <> 1 Then Exit Sub
    
    Sheet17.Unprotect ("password")
    Dim cl As Range
    If ActiveCell.Column = 8 Then
        For Each cl In ActiveCell.Cells
            If UCase(cl.Value) = UCase("Authorised") And cl.Column = 8 Then
                Range("a" & cl.row & ":h" & cl.row).Locked = True
            Else
                Range("a" & cl.row & ":h" & cl.row).Locked = False
            End If
        Next
    End If
    Sheet17.Protect ("password")
Exit Sub
'handle any errors
errHandle:
    MsgBox Err.Description, vbCritical, "Err No: " & Err.Number
End Sub




Private Sub CommandButton1_Click()
    LockRow
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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