VBA - Password protect sheet AND allow filtering

Gerald Higgins

Well-known Member
Joined
Mar 26, 2007
Messages
9,258
Hi all.

I'm having problems with a macro that should
a) Turn worksheet protection on for a specific worksheet
b) Require a password to turn it off
c) Allow filtering, while worksheet protection is on.

This is part of a larger AutoExecute macro that does a number of other things, which all work fine. These include turning protection on and off on other sheets (using a password), although none of the other sections deal with filtering.

This code turns protection on, and allows filtering. Which is good. But protection can then be manually turned off without requesting a password. Which is bad.
Code:
ActiveSheet.Protect "GERALD"
    ActiveSheet.EnableSelection = xlUnlockedCells
    ActiveSheet.Protect AllowFiltering:=True

This code turns protection on, and requires a password for manual unprotection. Which is good. But before protection is turned off, filtering can't be activated (even though the filter drop down arrows are clearly visible). Which is bad.
Code:
ActiveSheet.EnableSelection = xlUnlockedCells
    ActiveSheet.Protect AllowFiltering:=True
    ActiveSheet.Protect "GERALD"

I've tried every variation of this code that I can think of, but no luck yet.

Not sure if this is relevant, but all cells on the worksheet are locked, except for row 1, which contains the filter column headers.

As I'm still very much a beginner at VBA, there's probably something really basic that I am doing wrong. Can anyone point it out to me please ?

Thanks in advance for any help !
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try...

Code:
With ActiveSheet
    .Protect Password:="GERALD", AllowFiltering:=True
    .EnableSelection = xlUnlockedCells
End With
 
Upvote 0
Domenic - thanks for the reply and the very useful lesson for this VBA novice in how to use the WITH technique.

It worked perfectly ! Thanks again.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

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