Dynamic Hide & Unhide column

frozenfiree88

New Member
Joined
Feb 18, 2025
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

So I am new to VBA coding.

I wanted to dynamically unhide and hide the column based on the value in my cell C3. My problem is the first 1 is working, but when i choose another category it will hide all or other columns that I don't want to hide.

If I type TestA I wanted to hide my column Y to AB to only show my column X.

Then if I wanted to type testB I wanted to show column Y but hide column X and Z to AB.


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("C3"), Range(Target.Address)) Is Nothing Then
        Select Case Target.Value
        Case Is = "testA": Columns("Y:AB").EntireColumn.Hidden = True
                            Columns("A:V").EntireColumn.Hidden = False
                            
        Case Is = "testB": Columns("Z:AB").EntireColumn.Hidden = True
                            Columns("X").EntireColumn.Hidden = True
                           Columns("A:V").EntireColumn.Hidden = False
                          
        Case Is = "testC": Columns("X:Y").EntireColumn.Hidden = True
                            Columns("AA:AB").EntireColumn.Hidden = True
                           Columns("A:V").EntireColumn.Hidden = False
                          
        Case Is = "testD": Columns("X:Z").EntireColumn.Hidden = True
                             Columns("AB").EntireColumn.Hidden = True
                           Columns("A:V").EntireColumn.Hidden = False
                          
        Case Is = "testE": Columns("X:AA").EntireColumn.Hidden = True
                           Columns("A:V").EntireColumn.Hidden = False

        End Select
End If
End Sub


Thank you in advanced!
 
Try to always unhide all first and then hide required only, like (I changed the flocation of test cell for tests to not hide itself):
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("AC3"), Range(Target.Address)) Is Nothing Then
   Columns("A:AB").EntireColumn.Hidden = False
        Select Case Target.Value
        Case Is = "testA": Columns("Y:AB").EntireColumn.Hidden = True
                            
        Case Is = "testB": Columns("Z:AB").EntireColumn.Hidden = True
                            Columns("X").EntireColumn.Hidden = True
                          
        Case Is = "testC": Columns("X:Y").EntireColumn.Hidden = True
                            Columns("AA:AB").EntireColumn.Hidden = True
                          
        Case Is = "testD": Columns("X:Z").EntireColumn.Hidden = True
                             Columns("AB").EntireColumn.Hidden = True
                          
        Case Is = "testE": Columns("X:AA").EntireColumn.Hidden = True

        End Select
End If
End Sub
 
Upvote 0
Solution
Try to always unhide all first and then hide required only, like (I changed the flocation of test cell for tests to not hide itself):
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("AC3"), Range(Target.Address)) Is Nothing Then
   Columns("A:AB").EntireColumn.Hidden = False
        Select Case Target.Value
        Case Is = "testA": Columns("Y:AB").EntireColumn.Hidden = True
                           
        Case Is = "testB": Columns("Z:AB").EntireColumn.Hidden = True
                            Columns("X").EntireColumn.Hidden = True
                         
        Case Is = "testC": Columns("X:Y").EntireColumn.Hidden = True
                            Columns("AA:AB").EntireColumn.Hidden = True
                         
        Case Is = "testD": Columns("X:Z").EntireColumn.Hidden = True
                             Columns("AB").EntireColumn.Hidden = True
                         
        Case Is = "testE": Columns("X:AA").EntireColumn.Hidden = True

        End Select
End If
End Sub
Hello Kaper,

I marked your reply as solution. This works really well.

Appreciate the quick solution.

Thanks
 
Upvote 0

Forum statistics

Threads
1,226,771
Messages
6,192,924
Members
453,767
Latest member
922aloose

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