I'm getting a Run-time Error when I open a file that I saved with my worksheets protected. However, if I "unprotect" all of the worksheets and then "protect" them again, everything works fine.
I protect all of my worksheets using the following macro, which I have used for years without any issues:
When I open the file with all of the worksheets protected all of my other macros work fine. I appear to be only having issues with one line of code
I'm getting the Run-time Error '1004' "You cannot use this command on a protected sheets..." when I am trying to run the following macro:
I have simple GoTo macros that don't include an AutoFilter and they work fine. So, I can only assume it has something to do with that particular line. My "protect workbook" code allows for AutoFiltering and once I run the code to unprotect the worksheets and run it again to re-protect them, everything works fine.
I've tried reordering the code, changing the AutoFilter parameters, unlocking the AutoFilter range of cells and nothing seems to fix the issue other than unprotecting and re-protecting the worksheets.
Any help, advice or guidance would be greatly appreciated.
I protect all of my worksheets using the following macro, which I have used for years without any issues:
VBA Code:
Private Sub CommandButton1_Click()
Dim wSheet As Worksheet
Application.ScreenUpdating = False
For Each wSheet In Worksheets
If wSheet.ProtectContents = True Then
wSheet.Unprotect Password:=TextBox1.Text
Else
wSheet.Protect Password:=TextBox1.Text, DrawingObjects:=False, Contents:=True, AllowFormattingRows:=True, AllowFormattingColumns:=True, Scenarios:= _
True, UserInterfaceOnly:=True, AllowFiltering:=True
End If
Next wSheet
Application.ScreenUpdating = True
Unload Me
End Sub
When I open the file with all of the worksheets protected all of my other macros work fine. I appear to be only having issues with one line of code
VBA Code:
Range("MODB2BList").AutoFilter Field:=1, Criteria1:="<>"
VBA Code:
Sub GotoMODB2B()
shName = ActiveSheet.Name
Application.Goto Reference:="MODB2B", Scroll:=True
[B]Range("MODB2BList").AutoFilter Field:=1, Criteria1:="<>"[/B]
End Sub
I have simple GoTo macros that don't include an AutoFilter and they work fine. So, I can only assume it has something to do with that particular line. My "protect workbook" code allows for AutoFiltering and once I run the code to unprotect the worksheets and run it again to re-protect them, everything works fine.
I've tried reordering the code, changing the AutoFilter parameters, unlocking the AutoFilter range of cells and nothing seems to fix the issue other than unprotecting and re-protecting the worksheets.
Any help, advice or guidance would be greatly appreciated.