Writing VBA code to use Auto Filter while in a Protected Sheet or Protected Workbook

passin through

New Member
Joined
Apr 25, 2015
Messages
8
Is it possible to protect a sheet and/or workbook and assign a macro to a button which will Filter on and off the predetermined rows while the sheet is protected? When the sheet is unprotected, the macro runs exactly as desired.

On the Protect Sheet tab I have the SORT and AutoFilter boxes checked.

Currently using the following code:

ActiveSheet.Range("BCD_SORT_COLUMNS").AutoFilter Field:=2

When the routine is run, it halts and gives the following error message:

Run-time error ‘1004’:
You cannot use this command on a protected sheet. To use this command, you must first unprotect the sheet (Review tab, Changes group, Unprotect Sheet button). You may be prompted for a password.


The cells set up to use the FILTER are shown in the Advanced Filter dialog box as follows:
List range: $B$9:$C$301
Criteria range: $B$10:$FD$298
The cells named "BCD_SORT_COLUMNS" are $B$9:$C$301
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You will need to Unprotect the sheet as one of the first lines in your code, otherwise the code, as you have already found out, will fail !
 
Upvote 0
Thanks for your reply. I was keeping my fingers crossed hoping for a better out come.
My understanding was that it is not possible to unprotect and then protect a sheet from VBA code. Is there a way?
 
Upvote 0
You can protect and unprotect sheets via VBA:

If the sheet is NOT password protected:

Code:
'Unprotect my sheet
Sheets("Sheet1").Unprotect 

'Reprotect my sheet
Sheets("Sheet1").Protect

Do this if the sheet IS password protected:

Code:
'Unprotect my sheet
Sheets("Sheet1").Unprotect Password:="Pass"

'Reprotect my sheet
Sheets("Sheet1").Protect Password:="Pass"

Of course, swap out "Sheet1" for your target sheet.
 
Upvote 0
Aye, and it works like a charm of course. Its a work-around for what I see as Excels limits, but it gets me to where I've been trying to go. Thanks very much to both of you for your help.
 
Upvote 0
you do realise that you can include the Protect / Unprotect code into the existing macro ??
It doesn't have to be run separately !
 
Upvote 0
Thanks, the protect sheet works fine within the macro.
When the code executes, all check boxes are left unselected in the "PROTECT SHEET" dialog box except the "SELECT UNLOCKED CELLS". The check boxes are under the "ALLOW ALL USERS OF THIS WORKSHEET TO:".
What code would protect the worksheet and also allow for some options to be selected, such as the "USE AutoFilter"?
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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