Unable to Lock Cells when combined with an other intruction

Bwanna

New Member
Joined
Jan 10, 2018
Messages
13
Hi,

I have working code to Lock/Unlock a range of cells based on a DropDown selection ("PASSTHRU") of another cell. When I ADD a line to BLANK each cell (using ="") prior to Locking, I get the error: "Run-time error 1004 Unable to set locked property of the range class"

Any ideas?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub


' Lock/Unlock and Clear Cells when PASSTHRU Selected from Dropdown
    If Not Intersect(Target, Range("I6:I14")) Is Nothing And Target.Cells.Count = 1 Then
        ActiveSheet.Unprotect
            Dim i As Integer
            If Target.Value = "PASSTHRU" Then
                For i = 1 To 5
[B]                    Target.Offset(0, i) = ""[/B]
                    Target.Offset(0, i).Locked = True
                Next i
            Else
                For i = 1 To 5
                    Target.Offset(0, i).Locked = False
                Next i
            End If
        ActiveSheet.Protect
    End If
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
SOLVED:

I realized forgot to add Application.EnableEvents = False and Application.EnableEvents = True Statements.

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub


' Lock/Unlock and Clear Cells when PASSTHRU Selected from Dropdown
    If Not Intersect(Target, Range("I6:I14")) Is Nothing And Target.Cells.Count = 1 Then
        ActiveSheet.Unprotect
            Dim i As Integer
            If Target.Value = "PASSTHRU" Then
Application.EnableEvents = False
                For i = 1 To 5
                    Target.Offset(0, i) = ""
                    Target.Offset(0, i).Locked = True
                Next i
            Else
                For i = 1 To 5
                    Target.Offset(0, i).Locked = False
                Next i
Application.EnableEvents = True
            End If
        ActiveSheet.Protect
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,826
Messages
6,181,192
Members
453,021
Latest member
pingpong7117

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