Hide Worksheet except

garypea123

Board Regular
Joined
Mar 16, 2020
Messages
221
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a work book with around 20 tabs (work sheets)


At the beginning of my script I unhide them with

Application.ScreenUpdating = False

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

ws.Visible = xlSheetVisible

Next ws


But at the end of the script I would like to hide them all expect 3 or 4 (lets call them "Gary", "John","Jen","Karen". How do I hide all work books except the 4 I have listed please?

Thanks,
Gary
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How about
VBA Code:
Sub garypea()
   Dim Ws As Worksheet
   
   For Each Ws In Worksheets
      Select Case LCase(Ws.name)
         Case "gary", "john", "jen", "karen"
         Case Else
            Ws.Visible = xlSheetHidden
      End Select
   Next Ws
End Sub
 
Upvote 0
Hi,
untested but maybe

VBA Code:
Sub hidesheets()
    Dim ws          As Worksheet
    Dim arr         As Variant
    Dim KeepVisible As Boolean
    
    arr = Array("Gary", "John", "Jen", "Karen")
    For Each ws In ActiveWorkbook.Worksheets
        KeepVisible = Not IsError(Application.Match(ws.Name, arr, 0))
        ws.Visible = IIf(KeepVisible, xlSheetVisible, xlSheetVeryHidden)
    Next ws
End Sub

Do be mindful that at least one sheet must remain visible otherwise code will error if none of the tab names in the list exist

Dave
 
Upvote 0
Solution
It does not work.

Maybe its the Lcase? The worksheet in this one example are called "Macro Sheet" and "Data Sheet"

It seems to want to hide all worksheets, but then errors when it gets to the last one and cannot hide anymore :)

Sub AAAA_Hide()
Dim Ws As Worksheet

For Each Ws In Worksheets
Select Case LCase(Ws.Name)
Case "Macro Sheet", "Data Sheet"
Case Else
Ws.Visible = xlSheetHidden
End Select
Next Ws
End Sub

Thanks,
Gary
 
Upvote 0
Change the sheet names in the case statement to lower case only
 
Upvote 0
Hi,
untested but maybe

VBA Code:
Sub hidesheets()
    Dim ws          As Worksheet
    Dim arr         As Variant
    Dim KeepVisible As Boolean
   
    arr = Array("Gary", "John", "Jen", "Karen")
    For Each ws In ActiveWorkbook.Worksheets
        KeepVisible = Not IsError(Application.Match(ws.Name, arr, 0))
        ws.Visible = IIf(KeepVisible, xlSheetVisible, xlSheetVeryHidden)
    Next ws
End Sub

Do be mindful that at least one sheet must remain visible otherwise code will error if none of the tab names in the list exist

Dave
This does work, but why is it when I have ran the macro and then right click on tabs that Unhide is greyed out?
 
Upvote 0
Because that code makes the sheets very hidden, rather than just hidden.
 
Upvote 0
This does work, but why is it when I have ran the macro and then right click on tabs that Unhide is greyed out?

@Fluff kindly answered the question but if you don't want this then change part of code shown in BOLD to xlSheetHidden

Rich (BB code):
ws.Visible = IIf(KeepVisible, xlSheetVisible, xlSheetVeryHidden)

Dave
 
Upvote 0
Fantastic - DMT32 & Fluff.

Both Scripts work like a Dream - And I learned about the very hidden and hidden :)

Thank you both ever so much
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,803
Members
449,127
Latest member
Cyko

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