Forum Discussion

StephanieDowney's avatar
StephanieDowney
Copper Contributor
Apr 22, 2022

Macro for clearing filters on a protected sheet

I have an Excel file that I need to clear the filters from a column on a password protected sheet.

When someone opens the shared file (from OneSite), I would like for the filters in 2 of the columns cleared but need for the sheet to stay protected as it has been messed up by others and takes some work to fix.

I am ok if the protection is cleared then the filters cleared and the protection put back with the password.

 

Can someone write me a code to do this?

 

Thank you

3 Replies

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    StephanieDowney 

    Here's a little example with VBA code and file...hope it helps 🙂

     

     

    Option Explicit
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ActiveWorkbook.ActiveSheet.Unprotect ("1234")
    If ActiveSheet.AutoFilterMode Then
    If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
    End If
    ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=0
    ActiveWorkbook.ActiveSheet.Protect ("1234")
    End Sub

     

     It's just a bit the other way around... cancel all filters when you close it :).

     

    NikolinoDE

Resources