VBA Protection error

Colleen45

Active Member
Joined
Jul 22, 2007
Messages
495
Hi There
I have a worksheet that i would like to protect because of complex formulas
in this work sheet i have some VBA Code as follows:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 1 Or Target.Column = 18 Then
If Target.Value > 0 Then
Target.Offset(0, 2).Value = Date
Else
Target.Offset(0, 2).Value = ""
End If
End If
End Sub

And it works perfectly well
However when i protect the worksheet it produces an error as follows:

Runtime Error '1004'
Application Defined or Object-defined error

And yet as soon as the sheet protection is removed, it all works fine again.
How do i get around this.

Regards
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Target.Column = 1 Or Target.Column = 18 Then
        Target.Parent.Protect UserInterfaceOnly:=True
        If Target.Value > 0 Then
            Target.Offset(0, 2).Value = Date
        Else
            Target.Offset(0, 2).Value = ""
        End If
    End If
End Sub
 
Upvote 0
Not sure ..... if ur sheet is protected ..... how Worksheet_Change will be triggered ?

But u can try inserting code to unprotect the sheet at the start of macro,
something like

Sub macro()
ActiveSheet.Unprotect
.....
.........ur logic


ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

End sub
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,693
Members
449,117
Latest member
Aaagu

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