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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
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
That is it!! And with the change I at least somewhat understand the code. Can I write it no, but I can read it.

Thank you so much, this is exactly what I was looking for.
 
Upvote 0
Glad we could help & thanks for the feedback.
Thanks again Fluff,

Sidebar question while I have you is there a place/resource you would suggest for beginners in this. I can obviously just go to google and pick one of the million places but if you do have any sort of recommendation I would love to hear.
 
Upvote 0
Unfortunately I cannot recommend any site in particular, as I have learnt most of what I know from posting here & seeing how other people do things.
 
Upvote 0
Unfortunately I cannot recommend any site in particular, as I have learnt most of what I know from posting here & seeing how other people do things.
Seriously? That seems insane!

I guess I will just start searching more basic things here though and go from there. Thanks for all your help.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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