VBA Code for hiding columns based on multiple user inputs

Curdood

New Member
Joined
Sep 23, 2023
Messages
7
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi All,

New to VBA and looking for some help. I want to hide columns based on multiple user selections.

For this example, if a user wants to filter by Fiscal Quarter, Q1, Q2 (Row 3 values from column F onwards).... and give them the option to filter by Fiscal Year, FY24, FY23.... (Row 4 values from Column F onwards).
I've figured out how to hide by one input, in this case Quarter, but looking for some help on how to include the second.

B3 = Q1, Q2, Q3, Q4, All
B4 = FY24, FY23, FY22, FY21, All

Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Variant, y As String
If Target.Address = "$B$3" Then
y = Target.Value
With Range("F3:FF3")
Application.ScreenUpdating = False
.EntireColumn.Hidden = (y <> "All")
If y <> "All" Then
For Each x In .Cells
If x = y Then x.EntireColumn.Hidden = False
Next
End If
Application.ScreenUpdating = True
End With
End If
End Sub
 
Your image doesn't match your description, and changes structure from your original post. The financial years and quarters have swapped positions, also your data validation cells appear to be in the range B4:B6 and not B3:B5, with the FY cells in row 1. I'll write the code according to your image, not your description, unless I hear from you first. It seems to make more sense (given the new structure) to put the financial year option at the top of the three data validations.
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Your image doesn't match your description, and changes structure from your original post. The financial years and quarters have swapped positions, also your data validation cells appear to be in the range B4:B6 and not B3:B5, with the FY cells in row 1. I'll write the code according to your image, not your description, unless I hear from you first. It seems to make more sense (given the new structure) to put the financial year option at the top of the three data validations.
Thanks. You are correct, I quickly drew that up as an example and it is not presented well. But if you are to work off the image, columns F onwards make sense, and I need to make FY the first input cell, which can be at B3 or B4 (I Will adjust). Thanks!
 
Upvote 0
Please try the following code, which is based on this layout of your data:
select case.xlsm
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAO
1FY24FY24FY24FY24FY23FY23FY23FY23FY22FY22FY22FY22FY24FY24FY24FY24FY23FY23FY23FY23FY22FY22FY22FY22FY24FY24FY24FY24FY23FY23FY23FY23FY22FY22FY22FY22
2Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4
3ActualActualActualActualActualActualActualActualActualActualActualActualBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecast
4All
5All
6All
Sheet1
Cells with Data Validation
CellAllowCriteria
B4ListFY22,FY23,FY24,All
B5ListQ1,Q2,Q3,Q4,All
B6ListActual,Budget,Reforecast,All


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("B4:B6"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Dim x As String, y As String, c As Range, z As String
        x = Range("B4"): y = Range("B5"): z = Range("B6")
        
        Range("F:FF").EntireColumn.Hidden = False
        Select Case x
            Case "All"
                Select Case y
                    Case "All"
                        Select Case z
                           Case "All"
                        Case Else
                            For Each c In Range("F3:FF3")
                                If c <> z Then c.EntireColumn.Hidden = True
                            Next c
                        End Select
                    Case Else
                        For Each c In Range("F2:FF2")
                            If c <> y Then c.EntireColumn.Hidden = True
                        Next c
                End Select
            Case Else
                For Each c In Range("F1:FF1")
                    If c <> x Then c.EntireColumn.Hidden = True
                    If c.Offset(1) <> y And y <> "All" Then c.EntireColumn.Hidden = True
                    If c.Offset(2) <> z And z <> "All" Then c.EntireColumn.Hidden = True
                Next c
        End Select
    End If
Continue:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 1
Solution
Please try the following code, which is based on this layout of your data:
select case.xlsm
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAO
1FY24FY24FY24FY24FY23FY23FY23FY23FY22FY22FY22FY22FY24FY24FY24FY24FY23FY23FY23FY23FY22FY22FY22FY22FY24FY24FY24FY24FY23FY23FY23FY23FY22FY22FY22FY22
2Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4Q1Q2Q3Q4
3ActualActualActualActualActualActualActualActualActualActualActualActualBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetBudgetReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecastReforecast
4All
5All
6All
Sheet1
Cells with Data Validation
CellAllowCriteria
B4ListFY22,FY23,FY24,All
B5ListQ1,Q2,Q3,Q4,All
B6ListActual,Budget,Reforecast,All


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("B4:B6"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Dim x As String, y As String, c As Range, z As String
        x = Range("B4"): y = Range("B5"): z = Range("B6")
       
        Range("F:FF").EntireColumn.Hidden = False
        Select Case x
            Case "All"
                Select Case y
                    Case "All"
                        Select Case z
                           Case "All"
                        Case Else
                            For Each c In Range("F3:FF3")
                                If c <> z Then c.EntireColumn.Hidden = True
                            Next c
                        End Select
                    Case Else
                        For Each c In Range("F2:FF2")
                            If c <> y Then c.EntireColumn.Hidden = True
                        Next c
                End Select
            Case Else
                For Each c In Range("F1:FF1")
                    If c <> x Then c.EntireColumn.Hidden = True
                    If c.Offset(1) <> y And y <> "All" Then c.EntireColumn.Hidden = True
                    If c.Offset(2) <> z And z <> "All" Then c.EntireColumn.Hidden = True
                Next c
        End Select
    End If
Continue:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
Thank you, this is exactly what I needed and works great!

Much appreciated and thanks for the quick turnaround.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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