Hide Columns Based on Date in Each Column

Needtoknow

Board Regular
Joined
Apr 21, 2006
Messages
73
Hi,
I'm trying to Hide Columns based on the date in each column but only show the past 30 days. Going across the spreadsheet, I have the date in row D (starting at row D16, E16, .........AKY16).
When the user opens the file and selects the macro button, I want it to only show the columns for the past 30 days. I'm stuck.... is there an easier way?

Sub HideColumnsBasedOnDate()
Dim r As Range, c As Range
Application.ScreenUpdating = False

For Each c In r
If Range("D16").Value > Today Then
r.EntireColumn.Hidden = True
Else
r.EntireColumn.Hidden = False
End If
Next c
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Are the dates sequential? If yes, ascending or descending?
 
Upvote 0
Needtoknow,

Maybe...
Code:
Sub HideColumnsBasedOnDate()
Application.ScreenUpdating = False
Range("D16:AKY16").EntireColumn.Hidden = True
c = Application.WorksheetFunction.Match(CLng(Date), Range("A16:AKY16"), 0)
d = Application.WorksheetFunction.Max(c - 30, 4)
Range(Cells(16, d), Cells(16, c)).EntireColumn.Hidden = False
Application.ScreenUpdating = True
End Sub


OR MAYBE have it do it automatically as the sheet is activated ????

Code:
Private Sub Worksheet_Activate()
Application.ScreenUpdating = False
Range("D16:AKY16").EntireColumn.Hidden = True
c = Application.WorksheetFunction.Match(CLng(Date), Range("A16:AKY16"), 0)
d = Application.WorksheetFunction.Max(c - 30, 4)
Range(Cells(16, d), Cells(16, c)).EntireColumn.Hidden = False
Application.ScreenUpdating = True
End Sub

Hope that helps.
 
Upvote 0
It doesn't work. I get a run-time 1004 error when I use the following code.

Sub HideColumnsBasedOnDate()
Application.ScreenUpdating = False
Range("D16:AKY16").EntireColumn.Hidden = True
c = Application.WorksheetFunction.Match(CLng(Date), Range("A16:AKY16"), 0)
d = Application.WorksheetFunction.Max(c - 30, 4)
Range(Cells(16, d), Cells(16, c)).EntireColumn.Hidden = False
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I figured it out. I used code I found from another similar issue. Thanks for your help

Sub HideColumns2WeekView()
Dim wb As Workbook
Dim ws As Worksheet
Dim rngDates As Range
Dim datToday As Date

Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet 'run this macro from the sheet you want the hidden columns
Set rngDates = ws.Range("D16:AJD16") 'this range should match the range of dates

Application.ScreenUpdating = False

'make sure you unhide all columns before hiding them based on dates
rngDates.Select
Selection.EntireColumn.Hidden = False

datToday = Date

For Each cell In rngDates.Cells
If cell.Value <= (datToday - 14) Or cell.Value > datToday Then
cell.Select
Selection.EntireColumn.Hidden = True
End If
Next

ws.Range("A1").Select
Application.ScreenUpdating = True

MsgBox "Done!", vbOKOnly

End Sub
 
Upvote 0
Ntk,

Glad you are sorted.

Re my code producing an error......

I suspect that your dates are text rather than date serial?
In which case my Match statement will generate the error. This can be overcome but no need given that your code does the job.

It is not really an issue in such a small amount of code but it would be good practice to try and avoid unnecessary selections.
eg....

Rich (BB code):
Sub HideColumns2WeekView()
Dim wb As Workbook
Dim ws As Worksheet
Dim rngDates As Range
Dim datToday As Date


Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet 'run this macro from the sheet you want the hidden columns
Set rngDates = ws.Range("D16:AJD16") 'this range should match the range of dates


Application.ScreenUpdating = False


'make sure you unhide all columns before hiding them based on dates
rngDates.EntireColumn.Hidden = False


datToday = Date


For Each cell In rngDates.Cells
If cell.Value <= (datToday - 14) Or cell.Value > datToday Then
cell.EntireColumn.Hidden = True
End If
Next
ws.Range("A1").Select
Application.ScreenUpdating = True


MsgBox "Done!", vbOKOnly
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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