I'm trying to allow grouping in worksheets (EnableOutlining). Below is a typical example that allows it but it also resets all of the protection attributes as specified. Unfortunately I don't know what the existing attributes are. Is there a simple way of retaining the existing protection attributes when I set the grouping attribute?
Private Sub Workbook_Open()
' Modified from http://www.clickconsulting.com/forum/excel-support/grouping-protected-worksheet
' NOTE: It is not necessary to unprotect a worksheet to change the protection settings.
' Reference: https://exceloffthegrid.com/vba-code-worksheet-protection/
' https://stackoverflow.com/questions/37419714/unprotect-sheet-prompt-for-pw-only-when-allowfiltering-true?rq=1
Dim pw As String
pw = "Secret"
For Each ws In Sheets
With ws
If .ProtectContents = True Then
.Protect Password:=pw, UserInterfaceOnly:=True
.EnableOutlining = True
End If
End With
Next ws
End Sub
How do I retain existing settings rather than overwrite them?
.Protect Password:=pw, _
DrawingObjects:=False, _
Contents:=True, _
Scenarios:=False, _
UserInterfaceOnly:=True, _
AllowFormattingCells:=True, _
AllowFormattingColumns:=True, _
AllowFormattingRows:=True, _
AllowInsertingColumns:=True, _
AllowInsertingRows:=True, _
AllowInsertingHyperlinks:=True, _
AllowDeletingColumns:=True, _
AllowDeletingRows:=True, _
AllowSorting:=True, _
AllowFiltering:=True, _
AllowUsingPivotTables:=True
I'd welcome any comments on how to manage a hard-coded password too.