Allow to hide sheets in a locked workbook

Krulle1000

New Member
Joined
Feb 25, 2016
Messages
15
Hello,
How can I allow to hide sheets in my locked workbook? The code below doesn't work.
I receive Error 1004: Application-defined or object-defined error.
If I unlock my workbook than the code passes.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
For Each ws In ThisWorkbook.Worksheets
If ws.CodeName <> "Sheet3" Then
ws.Visible = xlVeryHidden
Next ws
end Sub
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Thanks,

Protecting the Workbook on Open after the Sheets have been hidden & Unprotect the Workbook immediately on Close does the trick.
Below the code to force the users to enable macros while the workbook is protected.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
ActiveWorkbook.Unprotect Password:="pass"
Sheet3.Visible = xlSheetVisible
For Each ws In ThisWorkbook.Worksheets
If ws.CodeName <> "Sheet3" Then
ws.Visible = xlVeryHidden
Else
ws.Name = "START"
End If
Next ws
End Sub

Private Sub Workbook_Open()
For Each ws In ThisWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
Sheet3.Visible = xlVeryHidden
Sheet4.Visible = xlVeryHidden
ActiveWorkbook.Protect Password:="pass", structure:=True, Windows:=False
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,016
Messages
6,122,700
Members
449,092
Latest member
snoom82

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