Need Macro to Hide Columns Based on a DropDown Menu Selection

crasherar82

New Member
Joined
Jul 23, 2018
Messages
4
I am struggling with a MACRO. I need to hide a range of columns based on the selection of a dropdown menu. Menu selections are 1-5 with the range of hidden columns changing based on the selection. I am pretty new at using macros and I am having a hard time. The macro runs but it hides columns starting from the target field. Here is the marco I input

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B5") = 1 Then
Application.Columns("E:L").Select
Application.Selection.EntireColumn.Hidden = True

ElseIf Range("B5") = 2 Then
Application.Columns("G:L").Select
Application.Selection.EntireColumn.Hidden = True

ElseIf Range("B5") = 3 Then
Application.Columns("I:L").Select
Application.Selection.EntireColumn.Hidden = True

ElseIf Range("B5") = 4 Then
Application.Columns("K:L").Select
Application.Selection.EntireColumn.Hidden = True
End If


End Sub

When exectued it hides columns B:L regardless of the selection in B5. Please help/advise.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi & welcome to MrExcel.
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address <> "$B$5" Or Target.Value = "" Then Exit Sub
   Range("E:L").EntireColumn.Hidden = False
   Range(Choose(Target.Value, "E:L", "G:L", "I:L", "K:L")).EntireColumn.Hidden = True
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address <> "$B$5" Or Target.Value = "" Then Exit Sub
   Range("E:L").EntireColumn.Hidden = False
   Range(Choose(Target.Value, "E:L", "G:L", "I:L", "K:L")).EntireColumn.Hidden = True
End Sub


That worked for options 1-4! When 5 is selected, nothing should be hidden. When I select 5 I get an error
(Run-Time Error 1004 : Method 'Range' of object'_Worksheet' failed).

Now I just need to figure out why it worked. I have one other macro I need to run that is similar, maybe you can help me with that too? :D
I have a yes/no field (B7). If no is selected, I need to hide Columns D, F, H, J, and L.

This would be easier if I understood this more. Any recommended resources I can review to get better at this?
Thanks!
 
Upvote 0
This will resolve the problem if 5 is selected.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address <> "$B$5" Or Target.Value = "" Then Exit Sub
   Range("E:L").EntireColumn.Hidden = False
   If Target.Value = 5 Then Exit Sub
   Range(Choose(Target.Value, "E:L", "G:L", "I:L", "K:L")).EntireColumn.Hidden = True
End Sub
Is the new request on the same sheet?
 
Upvote 0
Yes, it is the same sheet. And I lied, I need to hide columns D, F, H, J, L and O

Also, the code you sent fixed the problem. THANK YOU!!! :-D
 
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Value = "" Then Exit Sub
   If Not Intersect(Target, Range("B5")) Is Nothing Then
      Range("E:L").EntireColumn.Hidden = False
      If Target.Value = 5 Then Exit Sub
      Range(Choose(Target.Value, "E:L", "G:L", "I:L", "K:L")).EntireColumn.Hidden = True
   ElseIf Not Intersect(Target, Range("B7")) Is Nothing Then
      Range("D:D,F:F,H:H,J:J,L:L,O:O").EntireColumn.Hidden = LCase(Target.Value) = "no"
   End If
End Sub
 
Upvote 0
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Value = "" Then Exit Sub
   If Not Intersect(Target, Range("B5")) Is Nothing Then
      Range("E:L").EntireColumn.Hidden = False
      If Target.Value = 5 Then Exit Sub
      Range(Choose(Target.Value, "E:L", "G:L", "I:L", "K:L")).EntireColumn.Hidden = True
   ElseIf Not Intersect(Target, Range("B7")) Is Nothing Then
      Range("D:D,F:F,H:H,J:J,L:L,O:O").EntireColumn.Hidden = LCase(Target.Value) = "no"
   End If
End Sub

You are AMAZING!!! Thank you so much. You just saved me HOURS of work.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,910
Messages
6,181,678
Members
453,062
Latest member
blackyblack

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