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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,587
Office Version
  1. 365
Platform
  1. Windows
You will need to unprotect the workbook before trying to (un)hide the sheet & then reprotect it afterwards.
 

albert2

New Member
Joined
Jul 12, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,587
Office Version
  1. 365
Platform
  1. Windows
Yes, as I said you unprotect & reprotect within the code.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
60,028
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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?
 

albert2

New Member
Joined
Jul 12, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,587
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 

albert2

New Member
Joined
Jul 12, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
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!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,587
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,147,476
Messages
5,741,349
Members
423,656
Latest member
Medrok2021

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