Macro keeps re-protecting sheet without all of my lock preferences

elainaExcel

New Member
Joined
Jul 9, 2019
Messages
2
I need a macro to run upon opening excel that allows groups to expand and collapse while a sheet is protected. I am using the code below and it works, the issue is that when it re-protects my sheet is it only doing the first two defaulted lock preferences, "Allow all users of this worksheet to: select locked cells, select unlocked cells" I need the first five options "Allow all users of this worksheet to: select locked cells, select unlocked cells, format cells, format columns, format rows".

Please help! This is my first adventure into macros so exact code is very helpful.

[FONT=&quot]Sub Auto_Open()
[/FONT]

[FONT=&quot]Application.ScreenUpdating = False
[/FONT]

[FONT=&quot]For Each ws In Sheets
[/FONT]

[FONT=&quot]With ws
.Unprotect Password:=""
.Protect Password:="", UserInterfaceOnly:=True
.EnableOutlining = True
End With
[/FONT]

[FONT=&quot]Next ws[/FONT]
[FONT=&quot]Application.ScreenUpdating = True[/FONT]
[FONT=&quot]End Sub[/FONT]
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hello

Two ideas.
Google is awesome for learning Excel VBA.
Excel's macro recorder can be very helpful.

I just used the macro recorder (ALT-T-M-R) and it gives code that I think is what you want, and you will see too if you use the macro recorder.

Code:
Sub Macro1()
'
' Macro1 Macro
'


'
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True
End Sub
When you're finished recording, press the square icon in the status bar - bottom LHS corner of Excel, just to the right of the word "READY" - to stop the recording.

With this code snippet as a guide, please modify your code & post again if you're stuck.

HTH. regards, Fazza
 
Last edited:
Upvote 0
Thank you Fazza!


Here is my final code and it works!


Sub Auto_Open()

Application.ScreenUpdating = False

For Each ws In Sheets

With ws
.Unprotect Password:=""
.Protect Password:="", UserInterfaceOnly:=True
.EnableOutlining = True

ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True


End With

Next ws

Application.ScreenUpdating = True

End Sub


Hello

Two ideas.
Google is awesome for learning Excel VBA.
Excel's macro recorder can be very helpful.

I just used the macro recorder (ALT-T-M-R) and it gives code that I think is what you want, and you will see too if you use the macro recorder.

Code:
Sub Macro1()
'
' Macro1 Macro
'


'
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True
End Sub
When you're finished recording, press the square icon in the status bar - bottom LHS corner of Excel, just to the right of the word "READY" - to stop the recording.

With this code snippet as a guide, please modify your code & post again if you're stuck.

HTH. regards, Fazza
 
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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