VBA code to re-protect sheets of the workbook upon close and save that will also allow auto-filters to work

Musical2018

New Member
Joined
Apr 2, 2018
Messages
2
I am new to MrExcel and new to VBA and hope you will help me. I used the following code to protect my workbook sheets before close and upon save and it works great but I can't figure out how to write it to allow filters to still work on all tabs of the workbook when protected. There are several people using it and I need to be sure it doesn't get left unprotected but they need to be able to filter.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
        Const ccbsox As String = "ccbsox"

        For Each ws In ThisWorkbook.Worksheets
        ws.Protect Password:="ccbsox"
        Next ws
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet
        Const ccbsox As String = "ccbsox"

        For Each ws In ThisWorkbook.Worksheets
        ws.Protect Password:="ccbsox"
        Next ws
End Sub
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Welcome to MrExcel,

You can add a parameter to allow filtering, like this...

Code:
For Each ws In ThisWorkbook.Worksheets
   ws.Protect Password:="ccbsox", AllowFiltering:=True
Next ws

Be aware that this only allows the user of the protected sheet to use existing autofilters, it does not let them to add/remove autofilters to/from the worksheet.

btw, if you are wanting to use the Constant ccbsox that you declared at the top of your code to modify your password in one place, then you would use it without quotation marks.

Code:
For Each ws In ThisWorkbook.Worksheets
   ws.Protect Password:=[B][COLOR="#0000CD"]ccbsox[/COLOR][/B], AllowFiltering:=True
Next ws
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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