Show / Hide Sheets based on Field Value

jbunn

New Member
Joined
Feb 6, 2013
Messages
30
Hi -

I am trying to show / hide worksheets that start with a specific character based on a List selection. The following code will show or hide the worksheets however I can't figure out how to run the code based on a cell value.

For example, if B15 says "Show Detail" then show worksheets starting with "P". Consequently, if B15 says "Hide Detail, then hide worksheets starting with "P"

Sub ShowHideProject()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "P*" And ws.Visible = False Then
ws.Visible = xlSheetVisible
Else
If ws.Name Like "P*" And ws.Visible = True Then
ws.Visible = xlSheetHidden
End If
End If
Next ws
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try something like this:
Code:
Sub ShowHideProject()
    Dim ws As Worksheet
    If Range("B15") = "Show Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "P*" And ws.Visible = False Then
                ws.Visible = True
            End If
        Next ws
    
    ElseIf Range("B15") = "Hide Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "P*" And ws.Visible = True Then
                ws.Visible = False
            End If
        Next ws
    End If
End Sub
If you want the hiding/unhiding to happen automatically when you enter "Show/Hide Detail" in B15, then the macro can be modified as a Worksheet_Change event. Please let me know if this is what you would like. Cheers!
 
Upvote 0
Thanks Mumps!. Yes, I would like it to be a Change Event. Also, do I have to reference the worksheet name that corresponds to B15?
 
Upvote 0
Place this macro into the worksheet code module where you will be entering data in B15. Don't put it into a regular module. I would suggest that you put a validation list in B15 where you can choose "Hide Detail" or "Show Detail" from a drop down. This would eliminate spelling errors.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B15")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    If Target = "Show Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "P*" And ws.Visible = False Then
                ws.Visible = True
            End If
        Next ws
    
    ElseIf Target = "Hide Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "P*" And ws.Visible = True Then
                ws.Visible = False
            End If
        Next ws
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
OK, one last question. How do I specify multiple targets

For example
If B1 = Show Detail, then show worksheets targeting with M
If B15 = Show Detail, then show worksheets targeting with P (Got this already)
If B20 = Show Detail, then show worksheets targeting with U

Huge Thanks!
 
Upvote 0
Example correction

For example
If B1 = Show Detail, then show worksheets names starting with M
If B15 = Show Detail, then show worksheets names starting with P (Got this already)
If B20 = Show Detail, then show worksheets names starting with U
 
Upvote 0
This should do it:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B1,B15,B20")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    If Target.Address = "$B$1" And Target = "Show Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "M*" And ws.Visible = False Then
                ws.Visible = True
            End If
        Next ws
    
    ElseIf Target.Address = "$B$1" And Target = "Hide Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "M*" And ws.Visible = True Then
                ws.Visible = False
            End If
        Next ws
    End If
    
    If Target.Address = "$B$15" And Target = "Show Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "P*" And ws.Visible = False Then
                ws.Visible = True
            End If
        Next ws
    
    ElseIf Target.Address = "$B$15" And Target = "Hide Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "P*" And ws.Visible = True Then
                ws.Visible = False
            End If
        Next ws
    End If
    
    If Target.Address = "$B$20" And Target = "Show Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "U*" And ws.Visible = False Then
                ws.Visible = True
            End If
        Next ws
    
    ElseIf Target.Address = "$B$20" And Target = "Hide Detail" Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name Like "U*" And ws.Visible = True Then
                ws.Visible = False
            End If
        Next ws
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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