Show/Hide Specific (Changeable) Sheets based on Cell Value

justanotheruser

Board Regular
Joined
Aug 14, 2010
Messages
96
Hi guys,

I've been searching forums to no end to try to find a solution to this, but I've hit a road block. :(

I have a sheet called "Title" and cell E4 in this sheet is a drop down list which allows you to select certain codes, for example 210, 220 or 230.

Based on values in this cell, I would like to show certain worksheets, which are defined in another sheet, "Mapping", that contains the following values in Column C and D respectively.

Code Sheet Name Shown
210 PL_Workwear
210 BS_Workwear
220 PL_Cleanroom
220 BS_Cleanroom

I've managed to do this if I just enter the sheet names in the VBA code myself, but unfortunately it needs to be done so that a user who doesn't know how to use VBA can add more sheets and sheet names that will be shown by simply adding a line to the Mapping sheet, so they could add the line:

210 MM_Workwear

To the mapping sheet, and then if they select code 210 from the drop down box in Cell E4 in the Title sheet, then PL_Workwear, BS_Workwear and MM_Workwear will need to be shown.

My VBA isn't good enough to know how to do this - use dynamic ranges or VLOOKUPs with more than one value like on http://office.microsoft.com/en-us/excel-help/how-to-look-up-a-value-in-a-list-and-return-multiple-corresponding-values-HA001226038.aspx#_Toc273640147?

Any help or code you could provide would be very much appreciated! :)
 
Sorry, I omitted an End If

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long, i As Long
If Target.Address(False, False) = "E4" Then
    If Target.Value = "" Then
        For i = 1 To Worksheets.Count
            If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" Then Sheets(i).Visible = False
        Next i
        Exit Sub
    End If
    Application.ScreenUpdating = False
    For i = 1 To Worksheets.Count
        If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" Then Sheets(i).Visible = False
    Next i
    With Sheets("Mapping")
        LR = .Range("C" & Rows.Count).End(xlUp).Row
        For i = 3 To LR
            If Target.Value = .Range("C" & i).Value Then Sheets(.Range("D" & i).Value).Visible = True
        Next i
    End With
    Application.ScreenUpdating = True
End If
End Sub
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
No problem, in fact if anything I should thank you for making that mistake, now I know where the end if should have gone, learn a little every day! :)
 
Upvote 0
I tried to set it so that it allows another sheet, "Other" to be open no matter what you choose from the drop down list, see my code below, but it doesn't seem to allow the sheet "Other" - is there any alternative way to do this, as I have 20 sheets that should be open no matter what you choose from the drop down list, and then the other sheets are opened dependent on your drop down list choice.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long, i As Long
If Target.Address(False, False) = "E4" Then
    If Target.Value = "" Then
        For i = 1 To Worksheets.Count
            If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" And Sheets(i).Name <> "Other" Then Sheets(i).Visible = False
        Next i
        Exit Sub
    End If
    Application.ScreenUpdating = False
    For i = 1 To Worksheets.Count
        If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" And Sheets(i).Name <> "Other" Then Sheets(i).Visible = False
    Next i
    With Sheets("Mapping")
        LR = .Range("C" & Rows.Count).End(xlUp).Row
        For i = 3 To LR
            If Target.Value = .Range("C" & i).Value Then Sheets(.Range("D" & i).Value).Visible = True
        Next i
    End With
    Application.ScreenUpdating = True
End If
End Sub

Thanks again. :biggrin:
 
Upvote 0
Maybe like this

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long, i As Long
If Target.Address(False, False) = "E4" Then
    If Target.Value = "" Then
        For i = 1 To Worksheets.Count
            If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" And Sheets(i).Name <> "Other" Then Sheets(i).Visible = False
        Next i
        Exit Sub
    End If
    Application.ScreenUpdating = False
    For i = 1 To Worksheets.Count
        If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" And Sheets(i).Name <> "Other" Then Sheets(i).Visible = False
    Next i
    With Sheets("Mapping")
        LR = .Range("C" & Rows.Count).End(xlUp).Row
        For i = 3 To LR
            If Target.Value = .Range("C" & i).Value Then Sheets(.Range("D" & i).Value).Visible = True
        Next i
    End With
    Sheets("Other").Visible = True
    Application.ScreenUpdating = True
End If
End Sub
 
Upvote 0
That's a much better fix that mine, no need to enter the code twice! :) If on my table I was going to have something like this, where an entity does not need to open any particular new sheets (but would need the static pages open such as the Other page previously mentioned), is it possible to modify the code so that it doesn't do any particular action if the corresponding cell in Column D is blank - as in C11 and D11, without giving the Subscript out of range error?


Excel Workbook
CD
1EntitySheet Name
2**
3FA6_210PL_Workwear
4FA6_210BS_Workwear
5FA6_210PL_Cleanroom
6FA7_220PL_Workwear
7FA8S_220BS_Workwear
8FA89_230PL_Cleanroom
9KK_230PL_Workwear
10KK_230BS_Workwear
11WW_21*
12WW_11*
13AA_11*
Mapping


Thank you again - you deserve your MVP title! :)
 
Upvote 0
I think the basic concept would be something like if .Range("D" = "" Then Exit Sub but I don't think that's the right code for it, and where to put it. :confused:
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long, i As Long
If Target.Address(False, False) = "E4" Then
    If Target.Value = "" Then
        For i = 1 To Worksheets.Count
            If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" And Sheets(i).Name <> "Other" Then Sheets(i).Visible = False
        Next i
        Exit Sub
    End If
    Application.ScreenUpdating = False
    For i = 1 To Worksheets.Count
        If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" And Sheets(i).Name <> "Other" Then Sheets(i).Visible = False
    Next i
    With Sheets("Mapping")
        LR = .Range("C" & Rows.Count).End(xlUp).Row
        For i = 3 To LR
            If Target.Value = .Range("C" & i).Value And .Range("D" & i).Value <> "" Then Sheets(.Range("D" & i).Value).Visible = True
        Next i
    End With
    Sheets("Other").Visible = True
    Application.ScreenUpdating = True
End If
End Sub
 
Upvote 0
That works, thanks! I've created a dynamic range for my drop down list using the following:

Excel Workbook
ABCD
1**EntityAdditional Sheets to open
2****
31FA6_210FA6_210PL_Workwear
41FA7_220FA6_210BS_Workwear
51FA8S_220FA6_210PL_Cleanroom
62FA89_230FA7_220PL_Workwear
73KK_230FA8S_220BS_Workwear
84AA_11FA89_230PL_Cleanroom
95a1KK_230PL_Workwear
105*KK_230BS_Workwear
116*AA_11*
127*a1*
Mapping


If we look at Row B, where I have defined a name, called ENTA with the following: =OFFSET(Mapping!$B$3,0,0,COUNTA(Mapping!$B:$B),1) - in cell B10, even though the cell value is blank, because there is a formula in it, the cell is chosen as part of the dynamic range - is there a way to adjust the offset formula to fix this? The cells in Column B are formatted as [=0]"";General -- thanks again :)
 
Last edited:
Upvote 0
Dynamic ranges are not my forte - I suggest that you start a new thread for this different question.
 
Upvote 0
Okay, thank you - I have created a new thread as you suggested. I've changed the code so that if the first value says "Select Entity" (instead of it being blank), then it will actually need to hide all sheets other than the Title sheet, and then when you select one of the other values from the drop down list, it will open corresponding sheets. I tried just adding the Sheets("Other")... line code, but it didn't work - can it be done to hide all other sheets or alternatively if this is not possible hard code which other sheets (the static sheets that everyone can view no matter what they select from the drop down), including "Other"?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long, i As Long
If Target.Address(False, False) = "E4" Then
    If Target.Value = "Select Entity" Then
        For i = 1 To Worksheets.Count
            If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" And Sheets(i).Name <> "Other" Then Sheets(i).Visible = False
        Next i
        Exit Sub
    [COLOR="Red"]Sheets("Other").Visible = False[/COLOR]
    End If
    Application.ScreenUpdating = False
    For i = 1 To Worksheets.Count
        If Sheets(i).Name <> "Title" And Sheets(i).Name <> "Mapping" And Sheets(i).Name <> "Other" Then Sheets(i).Visible = False
    Next i
    With Sheets("Mapping")
        LR = .Range("C" & Rows.Count).End(xlUp).Row
        For i = 3 To LR
            If Target.Value = .Range("C" & i).Value And .Range("D" & i).Value <> "" Then Sheets(.Range("D" & i).Value).Visible = True
        Next i
    End With
    Sheets("Other").Visible = True
    Application.ScreenUpdating = True
End If
End Sub

I highlighted my change in red. :)
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,918
Members
449,195
Latest member
Stevenciu

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