Forum Discussion
Using Group-Ungroup in a protected Excel sheet
You have to create the grouping/outline while the worksheet is unprotected.
You have to protect the sheet in a special way when the workbook is opened.
Activate the Visual Basic Editor (Option+F11).
Double-click ThisWorkbook under Microsoft Excel Objects in the Project Explorer pane on the left.
Copy this code into the ThisWorkbook module:
Private Sub Workbook_Open()
With Me.Worksheets("Sheet1")
.Protect Password:="Secret", UserInterfaceOnly:=True
.EnableOutlining = True
End With
End Sub
Replace Sheet1 with the name of the relevant sheet, and replace Secret with the password that you used to protect the sheet (use "" if you didn't specify a password).
Save the workbook as a macro-enabled workbook (*.xlsm).
Make sure that you allow macros when you open the workbook.
- JytersMar 21, 2024Copper Contributor
- HansVogelaarMar 21, 2024MVP
If you want to do this for all worksheets in the workbook:
Private Sub Workbook_Open() Dim ws As Worksheet For Each ws In Me.Worksheets With ws .Protect Password:="Secret", UserInterfaceOnly:=True .EnableOutlining = True End With Next ws End Sub
If you want to apply it to several but not all worksheets, use something like this:
Private Sub Workbook_Open() Dim ws As Worksheet ' Modify the list of sheet names as needed For Each ws In Me.Worksheets(Array("Sheet2", "Sheet4", "Sheet6")) With ws .Protect Password:="Secret", UserInterfaceOnly:=True .EnableOutlining = True End With Next ws End Sub
- didierengelboschAug 02, 2024Copper Contributor
HansVogelaar thanks for this!
I am using a workbook with tens of tabs. I would like to apply the code to a selection of tabs without having to list them all in the code because 1)many 2)I might have to add and delete tabs and hence want to avoid having to hardcode them in the code. Is it possible to use a list of tabs without having to hardcode them individually in de code?
ps: I have a tab with a list of all tabs, and I have created a named range with the tabs I would like to protect. It if might help…
- Mick_KitcherMay 07, 2023Copper ContributorHans, maybe i didn't make it clear what i'm trying to do.
My sheet, which is named DATA (4), i hide and unhide a number of rows but i use the group/ungroup feature where the toggle box, for want of the correct term, sits in the left hand side of the rows. iI is that what i am trying to use without having to unlock the sheet each time, does that help or change anything?
Many thanks- HansVogelaarMay 07, 2023MVP
I have attached a very simple demo workbook. Does it work for you if you allow macros when you open it?
- Mick_KitcherMay 07, 2023Copper ContributorHi Hans, yes it does, i already have macros enabled, it works great, i will recheck my sheet to ensure i have not got some cells locked or something, i don't think i have but whatever it is the problem must be at my end. i will let you know and thank you once again for your help.
- Mick_KitcherMay 07, 2023Copper ContributorHi Hans, thank for the effort and i did exactly as you specified but i'm afraid it didn't work, as soon as i protect the sheet again it won't ungroup unless i unlock it.
- Jeffery_LeungJan 03, 2025Copper Contributor
I think I found a solution in a very old webpage:
https://www.mrexcel.com/board/threads/allow-grouping-option-on-a-protected-worksheet-workbook.479873/
Modified VBA Code as follow, and :
Sub Workbook_Open()
With worksheets("yoursheetname")
.Unprotect "PASSWORD"
.EnableOutlining = True
.Protect "PASSWORD", contents:=True, userInterfaceOnly:=True
End With
End Sub(I've bolded the "yoursheetname" and capitalized + bolded the two "password" so that it's easier to notice them)
I've also changed the original "private sub" to "sub", because it needs to be activated everytime I re-open the workbook, and thus I made a button that is linked to this module, and it does need to be clicked once after every re-opening of the workbook.
For some reason it does not work in the "private sub" format without the button-clicking, no idea why (still new to VBA), but anyhow, It is working fine for me for now (currently using Excel 2019).