How do I hide all worksheets that are NOT selected?

jh1234

New Member
Joined
Apr 4, 2011
Messages
19
I have many reports with workings on say 28 out of 30 tabs, and where the workings need to be hidden prior to distribution. It would be nice to be able to select the two tabs I want to keep and run a macro to hide the others. I'm looking for a generic macro that will work in any workbook, hiding all worksheets that are not selected.

I got as far as this but obviously it doesn't work...

for each ws in worksheets
if ws.selected = false then ws.visible = xlVeryHidden
next ws

Many thanks in advance for any responses!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
try this:

Code:
Sub hideAllWorksheetsButSelected()
    Application.ScreenUpdating = False
    For Each ws In Worksheets
        If ws.Name <> ActiveSheet.Name Then
            ws.Visible = xlVeryHidden
        End If
    Next
    For Each sh In ActiveWindow.SelectedSheets
        sh.Visible = True
    Next
End Sub
 
Upvote 0
Cool. Thanks. Works perfectly. I was surprised to see that you can use '...<> ActiveSheet.Name' when you've selected multiple sheets.
 
Upvote 0
Cool. Thanks. Works perfectly. I was surprised to see that you can use '...<> ActiveSheet.Name' when you've selected multiple sheets.
Yeah, I figured it doesn't matter how many sheets are selected, only 1 can be active at a time.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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