VBA - If Sheet is Hidden, Unhide - Else Hide

JonesyUK

Board Regular
Joined
Oct 11, 2005
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi All, hopefully this is a simple one for you....

Sheet1 contains a button

I need a Macro that does the following:

If the User Clicks the button then UNHIDE the "Database" Worksheet.
However, If the "Database" Sheet is already Visible
Then when the User Clicks the button, then HIDE the "Database" Worksheet.

Something along the lines of: if "Database" = visible, then hide "Database", else, unhide "Database" ...

Many thanks!
JonesyUK
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this code

Sub hideit()

If Sheets("Database").Visible = True Then Sheets("Database").Visible = False Else Sheets("Database").Visible = True

End Sub
 
Upvote 0
I know this is an old post - but this code is exactly what I'm looking for and I can't make it work!

Anyone have any ideas? Here's what it looks like with my sheet names:

Sub hideit()

If Sheets("Cost Estimate").Visible = True Then Sheets("Cost Estimate").Visible = False Else Sheets("Cost Estimate").Visible = True

End Sub
 
Upvote 0
I know this is an old post - but this code is exactly what I'm looking for and I can't make it work!

Anyone have any ideas? Here's what it looks like with my sheet names:

Sub hideit()

If Sheets("Cost Estimate").Visible = True Then Sheets("Cost Estimate").Visible = False Else Sheets("Cost Estimate").Visible = True

End Sub
The code you posted works fine for me. First, the obvious....is the name "Cost Estimate" spelled correctly on the tab? Next, is your sheet named "Cost Estimate" or "Cost Estimates"? Make sure the sheet name in the tab does not have a leading or trailing space and also make sure the space between the words on the tab is not actually two spaces next to each other.

Once you get your code working, here is a more compact way to write the macro...
Code:
Sub HideIt()
  Sheets("Cost Estimate").Visible = Not Sheets("Cost Estimate").Visible
End Sub
 
Upvote 0
First, the obvious....is the name "Cost Estimate" spelled correctly on the tab? Next, is your sheet named "Cost Estimate" or "Cost Estimates"? Make sure the sheet name in the tab does not have a leading or trailing space and also make sure the space between the words on the tab is not actually two spaces next to each other.

I thought I was all over this - turns out the other person editing my document changed the tab to Cost_Estimate! Fixed that and everything works just as I wanted.

Thank you so much for the super quick response - resolved hours of head scratching!!
 
Upvote 0
Sorry to be a pain, but I have another problem now!

How do I do this same thing for another button on the same sheet?!

I have another button called Risk Assessment, and a corresponding worksheet called Risk Assessment (I checked for spelling etc this time!)

If I change the name of the macro, I seem to lose the hide functionality.

Any thoughts?
 
Upvote 0
I have another button called Risk Assessment, and a corresponding worksheet called Risk Assessment (I checked for spelling etc this time!)

If I change the name of the macro, I seem to lose the hide functionality.
After you change the name of the macro, then you assign it to the button afterwards. If you assign the original name first, then the button points to the original macro (because it exists) no matter what you change the second macro's name to.
 
Upvote 0
Thanks again everyone, I managed to work it out!

Love this forum - everyone is so quick to help, it's really awesome! :)
 
Upvote 0

Forum statistics

Threads
1,215,109
Messages
6,123,136
Members
449,098
Latest member
Doanvanhieu

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