VBA to to selectively hide worksheets from drop down list

Nikosan73

New Member
Joined
Apr 13, 2017
Messages
6
I have a worksheet that acts as a Table of Contents, called "TOC".

There are dozens of worksheets behind it, which all have an identical format set of headers rows. One of those cells, cell B6, which contains a drop down lists of category, so that I can categorises each worksheet, like "Admin" , "Finance" etc.

I have created a script that dependant on the same dropdown list but this time in cell E3 on the "TOC" worksheet, shows only those worksheets that are of a certain category, for example "Admin". However the IF statements are hard coded in VBA as is the drop down lists, using the Data Validation functionality and a Range which lists the categories, called "Categories". It becomes cumbersome when I want to add a new category mainly because I have to add another IF statement.

I would like to update it so that the "worksheet hide script" can then work dynamically against the selection in cell E3 on the "TOC" worksheet.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I am almost there but I cannot get the worksheets to unhide when the Target cell is blank

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet

'Only monitor Cell C4
If Target.Address = Range("$C$4").Address Then

'If Target is Blank then apply Autofilter.
If IsEmpty(Range("c4").Value) = True Then
Range("b5:g5").AutoFilter

Else

'If Target is not Blank apply AdvancedFiler

Range("$B$5:$G$30").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("$c$3:$c$4"), Unique:=False

For Each ws In Worksheets
If ws.Name <> "TOC" Or ws.Range("A2") <> Worksheets("TOC").Range("C4").Value Then
ws.Visible = 0

If ws.Name = "TOC" Or ws.Range("A2") = Worksheets("TOC").Range("C4").Value Or IsEmpty(Sheets("TOC").Range("$C$4")) Then
ws.Visible = -1

End If
End If
Next ws


End If
End If
End Function
 
Upvote 0
Finally did it - a bit cumbersome, so happy for comments on improving it:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet​

'Only monitor Cell C4
If Target.Address = Range("$C$2").Address Then​

'Stop Screen Updating
With Application
.ScreenUpdating = False​
End With​

'If Target is Blank then apply Autofilter.
If IsEmpty(Range("c2").Value) = True Then​
Range("b3:g3").AutoFilter
'Then apply unhide Worksheet.​
For Each ws In ThisWorkbook.Sheets​
If (ws.Name <> "Template" And ws.Name <> "Definitions") Then​
ws.Visible = xlSheetVisible​
End If​
Next ws​

Else

'If Target is not Blank apply AdvancedFiler
Range("$B$3:$G$30").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("$c$1:$c$2"), Unique:=False​
For Each ws In Worksheets​
If ws.Name <> "TOC" Or ws.Range("A2") <> Worksheets("TOC").Range("C2").Value Then​
ws.Visible = 0​
If (ws.Name = "TOC" Or ws.Range("A2") = Worksheets("TOC").Range("C2").Value) Then​
ws.Visible = -1​

End If​
End If​
Next ws​

End If

Worksheets("TOC").Activate​

'Start Screen Updating​
With Application​
.ScreenUpdating = True​
End With​

End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,405
Messages
6,119,320
Members
448,887
Latest member
AirOliver

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