Please simply this macro - hide all sheets except specific set

LenPL

New Member
Joined
Aug 1, 2013
Messages
17
Hi,

The following macro works, however I am trying to clean up the code

Sub ShowSet1()
Dim xWs As Worksheet
Dim xName As String
xName = "Sheet5"
For Each xWs In Application.ActiveWorkbook.Worksheets
If xWs.Name <> xName Then
xWs.Visible = xlSheetHidden
End If
Sheets("Sheet1").Visible = True
Sheets("Sheet123").Visible = True
Sheets("Sheet23").Visible = True
Next
End Sub

Thank you in advance,

Len
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this

Sub Hide1A()
sHide = Array("Sheet1", "Sheet5", "Sheet23", "Sheet123")
For Each sName In sHide
Worksheets(sName).Visible = Visible
Next sName
End Sub
 
Upvote 0
Thanks for your Reply Trevor.

However this seems to hide the select sheets, rather than show.
 
Upvote 0
Maybe this
Code:
Sub ShowSet1()
Dim xWs As Worksheet, xName As String
xName = "Sheet5"
For Each xWs In Worksheets
If xWs.Name <> xName AND xWs.Name <> "Sheet1" AND xWs.Name <> "Sheet123" AND xWs.Name <> "Sheet23" Then
xWs.Visible = xlSheetHidden
End If
Next ws
End Sub
 
Upvote 0
another way maybe

Code:
Sub HideSheets()
    Dim NotHide As Variant, m As Variant
    Dim ws As Worksheet

    NotHide = Array("Sheet1", "Sheet5", "Sheet23", "Sheet123")

    For Each ws In Worksheets
        m = Application.Match(ws.Name, NotHide, False)
        ws.Visible = Not IsError(m)
    Next ws

End Sub

Dave
 
Upvote 0
Maybe this
Rich (BB code):
Sub ShowSet1()
Dim xWs As Worksheet, xName As String
xName = "Sheet5"
For Each xWs In Worksheets
If xWs.Name <> xName AND xWs.Name <> "Sheet1" AND xWs.Name <> "Sheet123" AND xWs.Name <> "Sheet23" Then
xWs.Visible = xlSheetHidden
End If
Next ws
End Sub

Thanks Michael, unfortunatley a "Complie error: Invalid Next control variable" reference pops up at the red text.
 
Upvote 0
Code:
[COLOR=#222222][FONT=Times New Roman]Sub ShowSet1()
Dim xWs As Worksheet, xName As String
xName = "Sheet5"
For Each xWs In Worksheets
If xWs.Name <> xName And xWs.Name <> "Sheet1" And xWs.Name <> "Sheet123" And xWs.Name <> "Sheet23" Then
xWs.Visible = 0
End If
Next xWs
End Sub
[/FONT][/COLOR]
 
Upvote 0
another way maybe

Code:
Sub HideSheets()
    Dim NotHide As Variant, m As Variant
    Dim ws As Worksheet

    NotHide = Array("Sheet1", "Sheet5", "Sheet23", "Sheet123")

    For Each ws In Worksheets
        m = Application.Match(ws.Name, NotHide, False)
        ws.Visible = Not IsError(m)
    Next ws

End Sub

Dave

Perfect!! Smooth and professional running, just what I was looking for.

Thanks Dave, this thread is now SOLVED.
 
Upvote 0
Perfect!! Smooth and professional running, just what I was looking for.

Thanks Dave, this thread is now SOLVED.

Many thanks for feedback it is very much appreciated & glad solution worked for you.

Just an aside, you could if you have other permutations of worksheets you need to keep visible make the code a common procedure and call if by passing the array as an argument.

Code:
Sub HideSheets(ByVal NotHide As Variant)
    Dim m As Variant
    Dim ws As Worksheet
    
    For Each ws In Worksheets
        m = Application.Match(ws.Name, NotHide, False)
        ws.Visible = Not IsError(m)
    Next ws
    
End Sub

and call it like this:

Code:
Sub ab()
    Dim wsArray As Variant
    wsArray = Array("Sheet1", "Sheet5", "Sheet23", "Sheet123")
    
    HideSheets NotHide:=wsArray
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,917
Messages
6,122,233
Members
449,075
Latest member
staticfluids

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