Hide columns based on date (Data validation)

Slizer6893

Active Member
Joined
Oct 25, 2013
Messages
271
Code:
Private Sub worksheet_Selectionchange(ByVal target As Range)Dim c As Range
Dim mycellschanged As Range
Dim mydatecells As Range
Dim CurrentDate As Range


Set mycellschanged = Intersect(target, Range("A2"))
Set mydatecells = Range("BJ5:CG5")
Set CurrentDate = Range("A2")
If mycellschanged Is Nothing Then Exit Sub
CurrentDate.Value = Month(CurrentDate.Value)
    For Each c In mydatecells
        
        If c.Value >= CurrentDate Then
                c.EntireColumn.Hidden = True
            Else
                c.EntireColumn.Hidden = False
        End If
    Next c


End Sub

At the moment this code isn't doing what I need it to. I have tweaked it a few times and I am having a few issues.

-A2 is going to be a drop down list that will specify date : Lets just say its months only not an actual date value that I know of

-BJ-CG are where months are across the top (So the most recent one would be September but months after, are still showing)

The goals: If my user changes the date in the drop down then all dates that are after that within my range will stay hidden (This allows my YTD column to automatically pull closer to the most recent month). Each month they can change the date and the new columns will appear.

My biggest issue is that using a data validation with dates doesn't seem to see September as greater than or equal to September since its using date values. (I do not want to change my headers) Can anyone think of a way that I can have it look at it like a month value instead? Month(Currentdate.value) didn't work?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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