Group/ Un-group in Protected sheet not working Correctly

kcroft88

New Member
Joined
Jun 23, 2016
Messages
37
Hi, 2nd post of the day :)

I am using a macro to allow users to expand and close grouped columns when the sheet is protected, and its works fine UNTIL I save, close it and go back in. When back in the workbook is locked (that's what I want), but when I try to expand the grouped columns it says "you cannot use this command in a protected sheet....". If I then go into the macro, press play and go back to the sheet it then works fine in protected mode. is there an addition to my code to make it run automatically so it works as soon as the sheet is open?


Private Sub Workbook_Open()
With Worksheets("Master Sheet")
.Protect Password:="password", Userinterfaceonly:=True
.EnableOutlining = True
End With
End Sub

Thanks in advance,
 

Some videos you may like

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,133
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
That code needs to be in the ThisWorkbook module of the workbook so that it works automatically.
 

kcroft88

New Member
Joined
Jun 23, 2016
Messages
37
Rather obvious now you point it out :) I have moved it and it works great now :)

For future reference for code to work automatically does it always need to be in ThisWorkbook module? or would that have worked where I had it if the top line was modified and merged with the one below?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,133
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
That code is a Workbook event, and must be in ThisWorkbook. Event routines related to a specific object need to be in the code module for that object - for example, Worksheet events like Worksheet_Change or Worksheet_Activate must be in the code module for the relevant worksheet.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,143
Messages
5,599,975
Members
414,354
Latest member
Flaxarn

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
Top