AutoFilter code returning error in Protected Worksheets

Lexcon07

New Member
Joined
Apr 21, 2020
Messages
33
Office Version
  1. 365
Platform
  1. Windows
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:

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:="<>"
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:

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.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,353
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
You will have to unprotect the worksheet that contains the Autofilter process
 

Lexcon07

New Member
Joined
Apr 21, 2020
Messages
33
Office Version
  1. 365
Platform
  1. Windows
But the only time I have an issue is when it is first opened with the worksheet protected. If I unprotect it and re-protect it after I have opened the file all of the macros works fine even with all of the worksheets protected.
 

Watch MrExcel Video

Forum statistics

Threads
1,132,890
Messages
5,655,820
Members
418,239
Latest member
RSadler11

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