Solved How to sort filters on a protected sheet? (1 Viewer)

goncalo

Member
Local time
Today, 12:48
Joined
May 23, 2023
Messages
51
Hello everyone,i've found myself with an issue that i have no idea on how to resolve
I have this file that is protected and people are not supposed to be delete,rewrite stuff on it,etc...
The thing is that i have filters on the first row and even though the sheet is protected and i don't want users to mess around with what's in there i still want them to be able to use the filters that i already placed in there,how can i achieve this?

I wrote this code but it still doesn't allow me to select the filters
Code:
Private Sub Workbook_Open()
 Dim wb As Workbook
 
    Set wb = Workbooks("dados.xlsm")
    
    Dim ws As Worksheet
    Set ws = wb.Worksheets("Dados")
    
    ws.protect Password:="567"
    ws.protect EnableAutoFilter = True
    ws.protect AllowFiltering = True
    ws.protect AllowSorting = True
    
    
End Sub




If anyone has any ideas i would appreciate it
 
Last edited:

goncalo

Member
Local time
Today, 12:48
Joined
May 23, 2023
Messages
51
well i managed to fix it lmao


Code:
Private Sub Workbook_Open()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim protectPassword As String

    Set wb = Workbooks("dados.xlsm")
    Set ws = wb.Worksheets("Dados")
    protectPassword = "567"

    If ws.ProtectContents Then
        ws.unprotect Password:=protectPassword
    End If

    ws.EnableAutoFilter = True
    ws.EnableOutlining = True


    If ws.AutoFilterMode Then
        ws.Sort.SortFields.Clear
        ws.Sort.SetRange ws.UsedRange
        ws.Sort.Header = xlYes
        ws.Sort.MatchCase = False
        ws.Sort.Orientation = xlTopToBottom
        ws.Sort.SortMethod = xlPinYin
        ws.Sort.Apply
    End If

    ws.protect Password:=protectPassword, UserInterfaceOnly:=True
End Sub
 

Users who are viewing this thread

Top Bottom