VBA - formatting/grouping with protected workbook

frostybear

New Member
Joined
Nov 30, 2023
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hi - I have a file that needs to be protected on multiple sheets but still allows for collapsing/uncollapsing grouped, filtering, inserting comments, formatting, etc. I have 2 sets of code that almost work, but one does everything except the grouping and the other does everything except the password protection. I'm going crazy!
gT5n4H3z19BNcMqZ4NQxDJevjG8jUydyF8oH_i9NHBzGc4QXdMSeSmQn6YJrWuvUOZWGKtg=s0-d-e1-ft


Option 1:
This does everything I need EXCEPT the password protection piece which I could not figure out how to correctly incorporate into this:

VBA Code:
Private Sub Workbook_Open()
    Dim wsh As Variant
    For Each wsh In Worksheets(Array("sheet 1", "sheet 2"))
        wsh.EnableOutlining = True
        wsh.EnableAutoFilter = True
        wsh.Protect UserInterfaceOnly:=True, _
            AllowFormattingCells:=True, _
            AllowFormattingColumns:=True, _
            DrawingObjects:=False, _
            Contents:=True
    Next wsh
End Sub

Option 2:
This one does everything I need EXCEPT the ability to collapse/uncollapse grouped columns/rows:

VBA Code:
Sub ProtectSheets()
    Dim wsh As Variant
    For Each wsh In Worksheets(Array("sheet 1", "sheet2"))
        wsh.Protect Password = "password"
        wsh.EnableOutlining = True
        wsh.EnableAutoFilter = True
        wsh.Protect UserInterfaceOnly:=True, _
            AllowFormattingCells:=True, _
            AllowFormattingColumns:=True, _
            DrawingObjects:=False, _
            Contents:=True
    Next wsh
End Sub

Hopefully this is an easy fix to get everything I need - I am definitely not an expert on this :) Thank you!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
How about:

VBA Code:
Private Sub Workbook_Open()
    Dim wsh As Variant
    For Each wsh In Worksheets(Array("sheet 1", "sheet 2"))
        wsh.EnableOutlining = True
        wsh.EnableAutoFilter = True
        wsh.Protect Password = "password", _
                            UserInterfaceOnly:=True, _
                            AllowFormattingCells:=True, _
                            AllowFormattingColumns:=True, _
                            DrawingObjects:=False, _
                            Contents:=True
    Next wsh
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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