Hide columns based on multiple available drop down list

Excelme at work

New Member
Joined
Feb 7, 2018
Messages
18
Hi all,

I have searched this forum for an answer but my understanding of VBA does not allow me to combine everything I have read.

I have created a spreadsheet which has the dates for an entire year listed across row from E3 : BK3

Cell D1 is a drop down menu with Jan - Dec and "Annual" as options.

I essentially want all columns except the corresponding month to hide when one is selected and all columns visible when "annual" is selected. Is this possible?

I used this forum to create a code which allows me to set "If January selected then hide all other columns"
However, as soon as I try to introduce an alternative outcome it does not work as required.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 And Target.Row = 1 And Target.Value = "January" Then
Columns("E:AI").Select
Selection.EntireColumn.Hidden = False
Else
Columns("E:NJ").Select
Selection.EntireColumn.Hidden = True
End If

If Target.Column = 4 And Target.Row = 1 And Target.Value = "February" Then
Columns("AJ:BK").Select
Selection.EntireColumn.Hidden = False
Else
Columns("E:NJ").Select
Selection.EntireColumn.Hidden = True
End If
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
the data validation list needs to be dates, formatted to text


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 And Target.Row = 1 Then
Dim c As Range, rng
Dim mymonth, month_val
month_val = Range("d1").Value

mymonth = Month(month_val)

Set rng = Range("e3:be3")
For Each c In rng
If Month(c) = mymonth Then
c.EntireColumn.Hidden = False
Else
c.EntireColumn.Hidden = True
End If
Next c
End If


End Sub
 
Upvote 0
the data validation list needs to be dates, formatted to text


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 And Target.Row = 1 Then
Dim c As Range, rng
Dim mymonth, month_val
month_val = Range("d1").Value

mymonth = Month(month_val)

Set rng = Range("e3:be3")
For Each c In rng
If Month(c) = mymonth Then
c.EntireColumn.Hidden = False
Else
c.EntireColumn.Hidden = True
End If
Next c
End If


End Sub

Thank you very much for this!
It now works perfectly, however the only item I can not see how to work is the option to include an "annual" or "whole year" function?
 
Upvote 0

Forum statistics

Threads
1,215,781
Messages
6,126,863
Members
449,345
Latest member
CharlieDP

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