Hide / Unhide worksheets

scotthannaford1973

Board Regular
Joined
Sep 27, 2017
Messages
110
Office Version
  1. 2010
Platform
  1. Windows
Hi all

I have a workbook with a huge number of worksheets that are optional, depending on the work they are being used for. I have a master worksheet that lists all of the worksheets and I'd like to add a button for each worksheet on that list that will hide/unhide the wokbook depending on whether or not it's used. I can record a macro to hide, another to unhide etc but I can't work out the code to "hide worksheet if visible, unhide if workbook is hidden, no error message!)". Can anyone advise?

cheers!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
How about
VBA Code:
   With Sheets("Sheet1")
      .Visible = Not .Visible
   End With
 
Upvote 0
If any of the sheets could be very hidden, use
VBA Code:
   With Sheets("Sheet1")
      .Visible = Choose(.Visible + 2, 0, -1, , -1)
   End With
 
Upvote 0
Thanks, Fluff - unfortunately when the worksheet is hidden, it brings up a run-time error; it also doesn't unhide the workbook if it is already hidden. I'd basically like one button that both/either hides or unhides a named worksheet.
 
Upvote 0
Which code are you referring to?
 
Upvote 0
Hi - actually this appears to work - thanks :)

Sub HideUnhideBCE()


If Sheet5.Visible Then
Sheet5.Select
ActiveWindow.SelectedSheets.Visible = False
Sheets("HOME").Select
Else
Sheet5.Visible = True
End If
End Sub
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0
I would suggest running this script to enter all sheet names in column A of the master sheet.
VBA Code:
Sub Enter_Worksheet_Names()
'Modified 10/21/2020 11:53:33 AM  EDT
Application.ScreenUpdating = False
Dim i As Long

For i = 1 To Sheets.Count
    Cells(i, 1).Value = Sheets(i).Name
Next
Application.ScreenUpdating = True
End Sub



Then my second script will run when ever you double click on a sheet name in column A.
The script will toggle the sheet from being visible or not visible.

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window


Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Modified  10/21/2020  12:21:15 PM  EDT
If Target.Column = 1 Then
Cancel = True
Sheets(Target.Value).Visible = Not Sheets(Target.Value).Visible
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

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