Collapse/Expand groupings in protected worksheet

Linki

New Member
Joined
Jun 8, 2023
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi all

Yesterday I had some help trying to make it so that people could expand and collapse existing groups within a protected worksheet.
Adding this code to workbook:

VBA Code:
Private Sub workbook_open()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
ws.Protect Password:="Test", userinterfaceonly:=True
ws.EnableOutlining = True
Next
End Sub

It worked well for my helper, but it did not work for me.
I'm using an O365 business license and excel is updated to the latest version.
We were using the same file for the test, and I made attempts on different computers - however, same version of excel.
I'm based in Europe, and the person assisting me is based in North America - could there be something regional?

When I try to expand or collapse the groups while the sheet is protected I keep getting faced with this error:
1695801323640.png


Would anyone have ideas as to what could be the issue?

Thank you! :)
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I tried this on two different computers. One domain joined with a bunch of security and one personal…in both cases I was able to expand and collapse groups on the protected sheet.
 
Upvote 0
I dont know if it could be related to my settings? These are my macro settings I've never modified these:
1695880543192.png
1695880618508.png
 
Upvote 0
After more testing...it turns out that Excel doesn't like having an ! at the start of a password. I haven't tried with other special characters, but I assume it would be true for those as well.
 
Upvote 0
Solution
Also, you should really be using:

Code:
For Each ws In ThisWorkbook.Sheets

not:

Code:
For Each ws In ActiveWorkbook.Sheets
 
Upvote 0
After more testing...it turns out that Excel doesn't like having an ! at the start of a password. I haven't tried with other special characters, but I assume it would be true for those as well.
This worked! Thank you! (at least after I found out the IT team had a setting in place that per default locked xlsm files downloaded).
 
Upvote 0
Also, you should really be using:

Code:
For Each ws In ThisWorkbook.Sheets

not:

Code:
For Each ws In ActiveWorkbook.Sheets
Thanks Rory!
Out of curiosity, using one or the other, what is the advantage / disadvantage? Very new to this.
 
Upvote 0
ThisWorkbook always refers to the workbook with the code in it, whereas Activeworkbook refers to whichever workbook is active at the time.
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,992
Members
449,094
Latest member
masterms

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