Go to http://www.contextures.com/excelfiles.html#UF0034 to get the sample file and code. In the UserForms section, look for UF0034 - Show Specific Sheets
In the workbook, select a description from a drop down list, and only the matching sheets will be visible. Or, select the ALL option, to see all the sheets again.
When the selection in the drop down list is changed, a macro runs. It shows any sheets whose names contain the selected item in the drop down list.
For example, choose "computer" and all sheets with "computer" in their name are visible, along with the Menu sheet. All other sheets are hidden.
This video shows how the drop down list is set up, and shows how the macros work.
0:19 Select a Sheet Type
0:56 Set Up the List
1:55 Find a Command
2:17 View the VBA Code
3:29 View the Macro Code
4:59 Get the Sample File
TRANSCRIPT FOR THE VIDEO
In this workbook, we have quite a few worksheets and I've set up a drop down list that we can use to hide everything except a specific kind of worksheet.
I'll show you how that works and then we can look at the code quickly in the background.
This is the menu sheet and there's a cell here that has a drop down list. If I click that, I can see, Computer, P&L and ALL.
I'll click on Computer and now we just have the menu sheet showing and the two sheets that had computer in their names.
I'll do the same for P&L, so there are the two sheets that are P&L and now I'll go back to looking at all the sheets and all the sheets, the admin sheets and instructions and everything is visible now.
To set this up, we have a data validation drop down list here, if I go to the data tab, data validation, we can see that there's a list and it's based on sheet types.
So I'll close that and that sheet types is on this admin list, I've just entered three items here, I typed all and I left a space character at the beginning of that, just so if I sort this list, that item will always go up to the top, so it's easy to find.
Then once the list was entered on this sheet, I created a table from that, so on the home tab, you can format as a table and select one of these styles for it and that means that now if I put another item at the bottom of this list, the list will expand automatically, we can see that the list is now grown and I can sort this.
And now if I go back to the menu, we've got admin and that just shows any sheet that has admin in its name.
So it's a quick way to work with your sheets if you want to either see a whole lot of them or just a small selection of your sheets so you can focus on those.
The sheets that are visible, change because a macro runs, if we make a change in this cell and to see that code, I'm going to right click on the menu tab, view code, and here is the worksheet change event.
So it's going to look at the target and that's the changed cell.
If the address of that cell is the same as the select type address and if we look at this cell, and look up in the name box here, that cell is named selecttype.
If we've changed that cell, then we're going to see what the value of the target cell is.
If we select it all, you can see this space there, it's going to run a macro called, showallsheets.
If we just clear out that cell, then nothing's going to happen, we can't show sheets if we don't have the text that's in the name of those sheets.
And, if they didn't select all or the cell isn't empty, then it's case else, so for every other situation, we're going to run a macro called, ShowSelSheets.
To see those macros, I'm going to right click here, definition, and that takes us here into module 1 and we can see the ShowAllSheets and there's a variable, ws, which is a worksheet.
So for each worksheet in the active workbook sheets, that worksheet, visible property is visible.
So every sheet becomes visible and for the other macro, ShowSelSheets, we again have a variable for the worksheet and one for the string, the str type, so whatever type we've selected here on the menu sheet in that select type range, so whatever that value is, that's what we're going to be looking for.
For each worksheet, in the ActiveWorkbook sheets, if we can find that name, so the strType in the worksheet name and we're using the InStr, the in string function for that, so if it does find that string in the name, then the sheet is visible, otherwise, unless the sheet is named, menu, we want to leave that visible, then that sheet will be hidden.
Those are the two different macros that will run based on making a change here.