Improved VBA Code to hide/unhide columns in a range

Addisonian

New Member
Joined
Oct 8, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
My VBA knowledge is very fresh and I imagine that shows in the code below. I have used the below code before to hide and unhide columns based on a small dropdown list. The list I am working off of here is a dropdown of a dropdown which means the total amount of columns I am looking at is 80+ and will continue to grow.

What I am trying to do is more or less is:

Hide columns (L:CU)=True [with the easy ability to move CU down the line] unless "row 7" of a column equals Target.Address (cell "I3")

Ideally done off a change event.



VBA Code:
If Target.Address = ("$I$3") Then
        If Target.Text = "Countryside" Then
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("M").EntireColumn.Hidden = False
            
        ElseIf Target.Text = "Yorktown" Then
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("N").EntireColumn.Hidden = False
            
        ElseIf Target.Text = "McLean" Then
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("O").EntireColumn.Hidden = False
    
        ElseIf Target.Text = "Bella Vista Traditional" Then
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("P").EntireColumn.Hidden = False
            
        ElseIf Target.Text = "New Haven" Then
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("Q").EntireColumn.Hidden = False



Thanks for any help.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Do you mean hide all columns from L to CU except those that have the same value as I3 in row 7 of that column?
 
Upvote 0
Do you mean hide all columns from L to CU except those that have the same value as I3 in row 7 of that column?
Exactly.

Thanks for the reply - Sorry for the misleading title it was a typo, should say "Help Improve..."
 
Upvote 0
I would use this script using Case instead of If.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  10/8/2021  2:06:11 PM  EDT

If Target.Address = ("$I$3") Then
        
        Select Case Target.Value
        Case "Countryside"
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("M").EntireColumn.Hidden = False
        Case "Countryside"
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("M").EntireColumn.Hidden = False
        Case "Yorktown"
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("N").EntireColumn.Hidden = False
        Case "McLean"
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("O").EntireColumn.Hidden = False
        Case "Bella Vista Traditional"
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("P").EntireColumn.Hidden = False
        Case "New Have"
            Columns("M:CT").EntireColumn.Hidden = True
            Columns("Q").EntireColumn.Hidden = False
            
End Select
End If
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Cl As Range
   
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "I3" Then
      Columns("L:CU").Hidden = True
         If Cl.Value = Target.Value Then Cl.EntireColumn.Hidden = False
      Next Cl
   End If
End Sub
 
Upvote 0
That doesn't seem to work - and unfortunately since I am pretty new to all this I am not sure why.

The debug pops a "Compile error: Next without For" if that helps?
 
Upvote 0
Oops, I missed a line
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Cl As Range
   
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "I3" Then
      Columns("L:CU").Hidden = True
      For Each Cl In Range("L7:CU7")
         If Cl.Value = Target.Value Then Cl.EntireColumn.Hidden = False
      Next Cl
   End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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