VBA to Hide/Unhide single Sheet

thelostscott

Board Regular
Joined
May 7, 2010
Messages
226
Hi all,

I'm in need of some help to get a sheet to Hide if visible and Unhide if hidden using a command button click.

I thought I could do something along the lines of:
Code:
Sub Hide_Unhide_Sheet1()
Application.ScreenUpdating = False
    If ActiveWorkbook.Sheets("Sheet1").Visible = xlVisible Then
    ActiveWorkbook.Sheets("Sheet1").Visible = xlSheetVeryHidden
    End If
    If ActiveWorkbook.Sheets("Sheet1").Visible = xlSheetVeryHidden Then
    ActiveWorkbook.Sheets("Sheet1").Visible = xlVisible
    End If 
Application.ScreenUpdating = True
End Sub

All this does though is keep it visible and doesn't do the hide bit.

Any ideas?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
If all you want to do is toggle, maybe

ActiveWorkbook.Sheets("Sheet1").Visible =not(ActiveWorkbook.Sheets("Sheet1").Visible )

Your code hides it with the first if, then unhides it with the second, so maybe if you need to use xlveryhidden, you could combine the 2 with if..then..else
 
Upvote 0
Thanks for the reply, in the end I solved it with this:
Code:
Sub Hide_Unhide_Sheet1()
Application.ScreenUpdating = False
If ActiveWorkbook.Sheets("Sheet1").Visible = True Then
        ActiveWorkbook.Sheets("Sheet1").Visible = xlVeryHidden
    Else
        ActiveWorkbook.Sheets("Sheet1").Visible = True
        
End If
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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