How to hide/unhide columns based on month chosen form a drop down list

Grev1

New Member
Joined
Dec 29, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi, I am trying to complete an attendance sheet by automatically hiding any column that is not in the month chosen. The drop down list is in cell B6 which is the months of the year (Date formatted to mmmm) and the days of the year are in D6:NE6.
I am trying to use the code below but nothing happens, I am not too familiar with VBA just what I've learned from googling and Youtube. Any help is greatly appreciated.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B6").Value = "January" Then
Columns("D:AH").EntireColumn.Hidden = False
Columns("AI:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "February" Then
Columns("D:AH").EntireColumn.Hidden = True
Columns("AI:BJ").EntireColumn.Hidden = False
Columns("BK:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "March" Then
Columns("D:BJ").EntireColumn.Hidden = True
Columns("BK:CO").EntireColumn.Hidden = False
Columns("CP:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "April" Then
Columns("D:CO").EntireColumn.Hidden = True
Columns("CP:DS").EntireColumn.Hidden = False
Columns("DT:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "May" Then
Columns("D:DS").EntireColumn.Hidden = True
Columns("DT:EX").EntireColumn.Hidden = False
Columns("EY:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "June" Then
Columns("D:EX").EntireColumn.Hidden = True
Columns("EY:GB").EntireColumn.Hidden = False
Columns("GC:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "July" Then
Columns("D:GB").EntireColumn.Hidden = True
Columns("GC:HG").EntireColumn.Hidden = False
Columns("HH:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "August" Then
Columns("D:HG").EntireColumn.Hidden = True
Columns("HH:IL").EntireColumn.Hidden = False
Columns("IM:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "September" Then
Columns("D:IL").EntireColumn.Hidden = True
Columns("IM:JP").EntireColumn.Hidden = False
Columns("JQ:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "October" Then
Columns("D:JP").EntireColumn.Hidden = True
Columns("JQ:KU").EntireColumn.Hidden = False
Columns("KV:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "November" Then
Columns("D:KU").EntireColumn.Hidden = True
Columns("KV:LY").EntireColumn.Hidden = False
Columns("LZ:NE").EntireColumn.Hidden = True
ElseIf Range("B6").Value = "December" Then
Columns("D:LY").EntireColumn.Hidden = True
Columns("LZ:NE").EntireColumn.Hidden = False
Else
Columns("D:NE").EntireColumn.Hidden = False
End If
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this:
I did two months for you. You do the remainder:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Modified  12/29/2021  10:56:53 AM  EST
If Target.Address = "$B$6" Then

Select Case Target.Value
    Case "January"
        Columns("D:AH").EntireColumn.Hidden = False
        Columns("AI:NE").EntireColumn.Hidden = True
    Case "March"
        Columns("D:AH").EntireColumn.Hidden = True
        Columns("AI:BJ").EntireColumn.Hidden = False
        Columns("BK:NE").EntireColumn.Hidden = True
        'You add more here for each month

    
    'End months here.
    Case Else
    Columns("D:NE").EntireColumn.Hidden = False


End Select
End If
End Sub
 
Upvote 0
You also do not need to use Entire Column you can just say:
Columns("D:AH").Hidden = True

I gave you a example. I like to have users see how to do it then do the remainder themself.
 
Last edited:
Upvote 0
I actually think you should use a Worksheet_Change
Not a selection change. The script actually runs when you enter the value in a cell
Using drop down list if you want. Not just moving the active cell to the range

Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  12/29/2021  12:48:32 PM  EST
If Target.Address = "$B$6" Then

Select Case Target.Value
    Case "January"
        Columns("D:AH").Hidden = False
        Columns("AI:NE").Hidden = True
    
    Case "February"
        Columns("D:AH").Hidden = True
        Columns("AI:BJ").Hidden = False
        Columns("BK:NE").Hidden = True
        
        'You add more Below here for each month

    
        'End months here.
    Case Else
    Columns("D:NE").Hidden = False
End Select
End If
End Sub
 
Upvote 0
Thanks for the reply. I've tried it and I'm not sure where I am going wrong but nothing is happening. Here is a capture of where I am at.
Capture.PNG





Capture2.PNG


Capture3.PNG
 

Attachments

  • Capture.PNG
    Capture.PNG
    33.2 KB · Views: 23
Upvote 0
You should be using the Worksheet_Change event not Worksheet_SelectionChange.
and the days of the year are in D6:NE6.
I don't think so. Maybe in rows 7 or 8, if so are they true dates ?
 
Upvote 0
Try it like
VBA Code:
Select Case Format(Target.Value, "mmmm")
 
Upvote 0
Solution
Show us exactly what you have in range "B6"
Do you Have January
Or Jan
Or
"January"
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

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