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
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Please try the following on a copy of your workbook:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("B3:B4"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Dim x As String, y As String, c As Range
        x = Range("B3"): y = Range("B4")
        
        Range("F:FF").EntireColumn.Hidden = False
        Select Case x
            Case "All"
                Select Case y
                    Case "All"
                    Case Else
                    For Each c In Range("F4:FF4")
                        If c <> y Then c.EntireColumn.Hidden = True
                    Next c
                End Select
            Case Else
                For Each c In Range("F3:FF3")
                    If c <> x Then c.EntireColumn.Hidden = True
                    If c.Offset(1) <> y And y <> "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
Please try the following on a copy of your workbook:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("B3:B4"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Dim x As String, y As String, c As Range
        x = Range("B3"): y = Range("B4")
       
        Range("F:FF").EntireColumn.Hidden = False
        Select Case x
            Case "All"
                Select Case y
                    Case "All"
                    Case Else
                    For Each c In Range("F4:FF4")
                        If c <> y Then c.EntireColumn.Hidden = True
                    Next c
                End Select
            Case Else
                For Each c In Range("F3:FF3")
                    If c <> x Then c.EntireColumn.Hidden = True
                    If c.Offset(1) <> y And y <> "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 works perfectly.
Is it possible to amend for a third, call it Z = Range ("B5")? I suppose there could be endless iterations, but am struggling to set up.
 
Upvote 0
Glad it worked out for you, and thanks for the feedback (y) :)

It should be possible - could you provide more details: criteria, range/row to assess?
Thanks.

B5 = Actual, Budget, Reforecast, All...
It would impact F5:FF5.
 
Upvote 0
Thanks.

B5 = Actual, Budget, Reforecast, All...
It would impact F5:FF5.
Could you provide a sample of how they would look across the sheet? I'm trying to visualise how they would relate to the Qs and FYs.
 
Upvote 0
1695642312310.png

The goal would be to input in the yellow highlighted cells, B3:B5 (and potentially more further on). Which would make it easier to hide cells F onwards based on those 3 fields.
 
Upvote 0
Could you provide a sample of how they would look across the sheet? I'm trying to visualise how they would relate to the Qs and FYs.
1695642312310.png


The goal would be to input in the yellow highlighted cells, B3:B5 (and potentially more further on). Which would make it easier to hide cells F onwards based on those 3 fields.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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