Hiding table columns based on header row name

Joined
Mar 22, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello!

Looking to get some assistance on this one. I'm creating a sheet where the end user would select from a drop down menu and the code will run and hide columns. The problem that I'm running into is I have to write code for each instance.

What I'm trying to accomplish is to have the code recognize and hide everything that is NOT what I picked in the dropdown menu, so I don't have to write the it over and over again.

For example, something like:

VBA Code:
ColumnToHide = Range("JobPicker")

Range("ColumnToHide_Status").EntireColumn.Hidden = True

I will also looking to incorporate multiple dropdown conditions:

If "Gym" AND "Math" are chosen, then hide everything else.




Please be gentle, I'm very new to VBA and have been hardcoding everything. :)


VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("JobPicker")) Is Nothing Then
        ActiveSheet.Columns.Hidden = False
        Select Case Target.Value
              
            Case "Math"
              Range("Science_Status").EntireColumn.Hidden = True
              Range("Science_Due_Date").EntireColumn.Hidden = True
              Range("Science_Priority").EntireColumn.Hidden = True
              
              Range("Arts_Status").EntireColumn.Hidden = True
              Range("Arts_Due_Date").EntireColumn.Hidden = True
              Range("Arts_Priority").EntireColumn.Hidden = True
              
              Range("Gym_Status").EntireColumn.Hidden = True
              Range("Gym_Due_Date").EntireColumn.Hidden = True
              Range("Gym_Priority").EntireColumn.Hidden = True
              
            Case "Science"
              Range("Arts_Status").EntireColumn.Hidden = True
              Range("Arts_Due_Date").EntireColumn.Hidden = True
              Range("Arts_Priority").EntireColumn.Hidden = True
              
              Range("Math_Status").EntireColumn.Hidden = True
              Range("Math_Due_Date").EntireColumn.Hidden = True
              Range("Math_Priority").EntireColumn.Hidden = True
              
              Range("Gym_Status").EntireColumn.Hidden = True
              Range("Gym_Due_Date").EntireColumn.Hidden = True
              Range("Gym_Priority").EntireColumn.Hidden = True
              
            Case "Gym"
              Range("Science_Status").EntireColumn.Hidden = True
              Range("Science_Due_Date").EntireColumn.Hidden = True
              Range("Science_Priority").EntireColumn.Hidden = True
              
              Range("Arts_Status").EntireColumn.Hidden = True
              Range("Arts_Due_Date").EntireColumn.Hidden = True
              Range("Arts_Priority").EntireColumn.Hidden = True
              
              Range("Math_Status").EntireColumn.Hidden = True
              Range("Math_Due_Date").EntireColumn.Hidden = True
              Range("Math_Priority").EntireColumn.Hidden = True
              
            Case "Arts"
              Range("Gym_Status").EntireColumn.Hidden = True
              Range("Gym_Due_Date").EntireColumn.Hidden = True
              Range("Gym_Priority").EntireColumn.Hidden = True
              
              Range("Science_Status").EntireColumn.Hidden = True
              Range("Science_Due_Date").EntireColumn.Hidden = True
              Range("Science_Priority").EntireColumn.Hidden = True
              
              Range("Math_Status").EntireColumn.Hidden = True
              Range("Math_Due_Date").EntireColumn.Hidden = True
              Range("Math_Priority").EntireColumn.Hidden = True
            
              
              
        End Select
    End If
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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