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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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
 

hsk

Well-known Member
Joined
Oct 19, 2006
Messages
567
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
 

Forum statistics

Threads
1,181,658
Messages
5,931,271
Members
436,785
Latest member
KingGideon

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
Top