Home » excel » excel – How to set a protection on worksheets but enable outlining and filter, being available after restart?

excel – How to set a protection on worksheets but enable outlining and filter, being available after restart?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to send a price list to the customer and need to protect worksheets ( only two). However, I want to allow grouping/outlining and filtering data. As the worksheets are supposed to be sent to the customer, I don’t want to bother him with anything.

I tried to set a password with the required criteria, though anytime I reopen the workbook, the grouping/outlining is locked.

What I can think of is giving a try to PivotTable?

Option Explicit
Sub protection()

Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Sheet1")

Dim ws2 As Worksheet
Set ws2 = ThisWorkbook.Sheets("Sheet2")

 Dim strPassword As String: strPassword = "***"


    With ws1

        .Protect Password:=strPassword, UserInterfaceOnly:=True, AllowFiltering:=True
        .EnableOutlining = True

    End With


    With ws2

        .Protect Password:=strPassword, UserInterfaceOnly:=True, AllowFiltering:=True

        .EnableOutlining = True

    End With


End Sub
How to&Answers:

You need to reset the worksheets protection for UserInterfaceOnly to work after the workbook is reopened.

Private Sub Workbook_Open()
    protection
End Sub    

Sub protection()
    Const Password = "***"        
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets(Array("Sheet1", "Sheet2"))
        With ws  
            .Protect Password:=Password , UserInterfaceOnly:=True, AllowFiltering:=True
            .EnableOutlining = True
        End With
    Next    
End Sub