VBA- Hide Multiple Tabs (sheets) based on list

MrSourApple

New Member
Joined
Oct 23, 2015
Messages
17
I have a a few lists on the tab "Master" in cells D3:D10, D12:D16, and D18:D24. These lists have the names of workbook tabs that I want hidden. Is there a quick way to hide all of these tabs so I don't have to do it manually?

This is the code I have to hide the first tab but would like a quick way to hide these tabs rather than copying this line for each tab:

Code:
Sheets(Sheets("Master").Range("D3").Value).Visible = False
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this macro:
Code:
Sub MyHideSheets()

    Dim myRange As Range
    Dim cell As Range
    
    Set myRange = Sheets("Master").Range("D3:D10, D12:D16, D18:D24")
    
    For Each cell In myRange
        If cell.Value <> "" Then
            Sheets(cell.Value).Visible = False
        End If
    Next cell
    
End Sub
 
Upvote 0
Thank you this worked perfectly, and since it is so simple I should be able to use it in other workbooks as well!
 
Upvote 0
Great! Glad to help!

One thing you may want to account for is what to do if there is a name in your list that does not match a sheet name. Currently, you would get an error. If you simply want to ignore those errors, you can do so with this slight modification:
Code:
Sub MyHideSheets()

    Dim myRange As Range
    Dim cell As Range
    
    Set myRange = Sheets("Master").Range("D3:D10, D12:D16, D18:D24")
    
    On Error Resume Next
    For Each cell In myRange
        If cell.Value <> "" Then
            Sheets(cell.Value).Visible = False
        End If
    Next cell
    On Error GoTo 0
    
End Sub
or else we could add some "error trapping" if you wanted it to return a message telling you that it cannot find a specific sheet name.
 
Upvote 0
Thank you for the error handling thought. I actually figured that out pretty quickly so I added that in right away.
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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