Unable to Hide/Unhide Macro because of Workbook structure protection

albert2

New Member
Joined
Jul 12, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello Excel expert!

I came up with this group because I can find my solution through google. I have a workbook-protected STRUCTURE and I also have a macro that hides and unhides one worksheet.
When I run the macro to hide/unhide a sheet an error pop up. I hope you could help me with this. below is the screenshot for your reference. below is my hide/unhide code.

Sub Unhide_SD_Sheet()

If Sheet2.Visible Then Sheet2.Visible = xlSheetHidden Else: Sheet2.Visible = xlSheetVisible

If Sheet2.Visible Then MsgBox ("Supporting Documentation Sheet Now Visible") Else MsgBox ("Supporting Documentation Sheet Now Hidden")


End Sub

1626113324255.png
 

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.
You will need to unprotect the workbook before trying to (un)hide the sheet & then reprotect it afterwards.
 
Upvote 0
You will need to unprotect the workbook before trying to (un)hide the sheet & then reprotect it afterwards
I want my workbook to be protected. Is there a way of running macro even though it's protected?
 
Upvote 0
Yes, as I said you unprotect & reprotect within the code.
 
Upvote 0
I want my workbook to be protected. Is there a way of running macro even though it's protected?
As Fluff said, you need to unprotect it first.
If you were able to run it without doing that, the protection would pretty much be worthless, right?
 
Upvote 0
As Fluff said, you need to unprotect it first.
If you were able to run it without doing that, the protection would pretty much be worthless, right?
I have 35 sheets in 1 workbook and they are all protected. Now, when I run the codes (hide/unhide) that the error comes out. I need to keep it protected for the rest of the sheets.
 
Upvote 0
Try it like
VBA Code:
Sub Unhide_SD_Sheet()
ThisWorkbook.Unprotect "Password"
If Sheet2.Visible Then Sheet2.Visible = xlSheetHidden Else: Sheet2.Visible = xlSheetVisible

If Sheet2.Visible Then MsgBox ("Supporting Documentation Sheet Now Visible") Else MsgBox ("Supporting Documentation Sheet Now Hidden")
ThisWorkbook.Protect "Password"

End Sub
 
Upvote 0
Try it like
VBA Code:
Sub Unhide_SD_Sheet()
ThisWorkbook.Unprotect "Password"
If Sheet2.Visible Then Sheet2.Visible = xlSheetHidden Else: Sheet2.Visible = xlSheetVisible

If Sheet2.Visible Then MsgBox ("Supporting Documentation Sheet Now Visible") Else MsgBox ("Supporting Documentation Sheet Now Hidden")
ThisWorkbook.Protect "Password"

End Sub
Oh wow, it worked. I never expect it's gonna be that simple. Thank you so much I appreciate it!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

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