dynamic protecting/unprotecting

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
655
Good Morning,

I have a workbook that initially has four sheets, two of which are very hidden. However, almost every time the workbook is opened, a new sheet is going to be added, which will be named either "noon"/"noon#"/"Arrival" (at the end). The number of "Noons" is dynamic, sometimes there's 15 Noons (Noon, Noon2, Noon3, etc) and sometimes none. There is always one "Arrival" at the very end. The Noons and Arrival are generated and formatted via vba. I'd like to protect the sheets so the user can't delete anything- I figure this is easy as I could just add the code into the end of the macro that creates the sheet. However, I'd like to have a button on one of the hidden sheets (named "Developer") that unlocks all of the protected sheets. If the number of sheets is changing, how would I do this? My limited experience for this macro would be to record myself protecting a sheet and then cleaning up the recorded macro- but that's only good for the sheets created and not those that are not....

Thanks!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi,

To unprotect all sheets:

Code:
Dim sh as worksheet
For each sh in activeworkbook.worksheets
sh.unprotect
Next sh

When in VBA, click on Thisworkbook below the sheets in left column and paste

Code:
Private Sub Workbook_Open()
Dim sh As Worksheet


For Each sh In ActiveWorkbook.Worksheets
    sh.Protect Password:="[COLOR=#0000ff]YourPassword[/COLOR]", UserInterFaceOnly:=True
Next sh
End Sub

to protect all sheets when opening the workbook. Note that userinterfaceonly makes it protected for users but not the macro (so you don't need protect/unptotect sheets in your macros).

If you are like me and often forget to protect/hide sheets after usage for the next user, you might appreciate a macro doing it for you on workbook opening by adding hiding status to the previous macro:

Code:
Private Sub Workbook_Open()
Dim sh As Worksheet
    For Each sh In ActiveWorkbook.Worksheets
        sh.Protect Password:="[COLOR=#0000ff]YourPassword[/COLOR]", UserInterFaceOnly:=True
        If sh.Name = "Developer" Then
            sh.Visible = xlSheetHidden
            Else
            sh.Visible = xlSheetVisible
        End If
        If sh.Name = "[COLOR=#0000ff]veryhiddensheet1[/COLOR]" Or sh.Name = "[COLOR=#0000ff]verhiddensheet2[/COLOR]" Then
            sh.Visible = xlSheetVeryHidden
        End If
    Next sh
End Sub

so you can unhide and unprotect all, work and save, the next user will always have the right protected and hidden sheets
 
Last edited:
Upvote 0
I like the way you think!

So based on what you said, I put this to the button
Code:
Sub UnProtector()Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
sh.Unprotect
Next sh
End Sub

And then I added your last piece of code there. I realize it's kind of redundant because, like you, I just had this piece in there for the same idea!
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'This is the last event to run as Excel is closing


Sheets("Notes").Visible = xlVeryHidden
Sheets("Developer").Visible = xlVeryHidden




End Sub

Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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