Use Button to Hide/Unhide Tabs Based on Cell Value

GBM15228

New Member
Joined
Jul 22, 2015
Messages
2
Hello:

I am almost completely new to VBA and trying to add code to an excel workbook to automate the display of tabs based on a cell condition.

On a main tab, let's call it 'CONTROL', I list a series of 9 tabs which are hidden in the workbook. These are named alphabetically from 'A' thru 'I'. Next to this list of tab names I have a conditional cell which reads "Yes" or "No". To illustrate:

Col. A Col. B
Tab Name: Review?:
A Yes
B No
C Yes
D Yes
E No
F No
G No
H No
I No


<tbody>
</tbody>
I would like to include two buttons on this worksheet.

When you click the first button, it will unhide each tab from column A noted "Yes" in column B. So, using the above example, clicking the button would unhide only tabs 'A', 'C', and 'D'. Of course, the tabs to be unhidden could change since whether a given tab is noted "Yes" or "No" is dependent on a separate formula I have written to each cell. So, if you were to click the button once it would display tabs 'A', 'C' and 'D'. If the conditions then change to only require review of tabs 'A' and 'C', clicking the button again should display only tabs 'A' and C'.

The second button would hide all tabs 'A' thru 'I' indiscriminately.

After googling and searching this forum, I cannot determine the appropriate code which will accomplish these tasks.
Any help is appreciated.

Thanks,
Greg
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hello!

EDIT: These macros should work if your sheet-names are in cells A1:A9 and Yes/No markers in B1:B9.
EDIT:EDIT: Oh, I used "Sheet1" instead of "CONTROL".. you'll figure it out.

Try these two macros:

Show sheets that are marked with "Yes":

Code:
Sub showtabs()

Dim rng As Range
Set rng = ThisWorkbook.Sheets("Sheet1").Cells.Range("A1:A9")
For Each c In rng
If c.Offset(0, 1).Value = "Yes" Then
Sheets(c.Value).Visible = True
End If
Next
End Sub

And Hide all sheets:
Code:
Sub hidetabs()
Dim rng As Range
Set rng = ThisWorkbook.Sheets("Sheet1").Cells.Range("a1:A9")
For Each c In rng
ThisWorkbook.Sheets(c.Value).Visible = False
Next
End Sub

You should change the ranges to match your workbook, then just assign the macros to buttons.

Hope it works as you wanted.



Matias
 
Last edited:
Upvote 0
Slight change for first macro, I forgot it was supposed to hide unwanted sheets if something changes.

Code:
Sub showtabs()
Dim rng As Range
Set rng = ThisWorkbook.Sheets("Taul1").Cells.Range("A1:A4")
For Each c In rng
If c.Offset(0, 1).Value = "Yes" Then
Sheets(c.Value).Visible = True
Else:
Sheets(c.Value).Visible = False
End If
Next
End Sub
 
Upvote 0
Hi Matias (Runsk1) - That worked like a charm. Thank you for the code and for the instructions! Have a great day.
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,666
Members
449,114
Latest member
aides

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