Hi,
I am getting an 'Object required' error on the line
Call HideColumnsOutsideRange(ws, startOfWeek, endOfWeek)
when I run the ShowCurrentWeek macro but not when I run the ShowCurrentMonth macro.
Any ideas?
Regards,
GW
Option Explicit
Sub HideColumnsOutsideRange(ws as Worksheet, startDate As Date, endDate As Date)
Dim lastCol As Long
Dim c As Long
Dim hdr As Variant
Dim dt As Date
' Find last used column in row 1
Application.ScreenUpdating = False
ws.Cells.EntireColumn.Hidden = False
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
For c = 1 To lastCol
hdr = ws.Cells(1, c).Value
If IsDate(hdr) Then
' Normalize to pure date (drop time / timezone artifacts)
dt = DateSerial(Year(CDate(hdr)), Month(CDate(hdr)), Day(CDate(hdr)))
' Debug.print "dt: " & dt & " start: " & startDate & " end: " & endDate
' Hide if outside the given range
If (dt < startDate) Or (dt > endDate) Then
ws.Columns(c).Hidden = True
End If
Else
ws.Columns(c).Hidden = True
End If
Next c
Application.ScreenUpdating = True
End Sub
Sub ShowCurrentMonth()
Dim ws As Worksheet
Dim today As Date, startOfMonth As Date, endOfMonth As Date
Set ws = ActiveSheet
today = DateValue(Now)
startOfMonth = DateSerial(Year(today), Month(today), 1)
endOfMonth = DateSerial(Year(today), Month(today) + 1, 0)
Call HideColumnsOutsideRange(ws, startOfMonth, endOfMonth)
End Sub
Sub ShowCurrentWeek()
Dim ws As Worksheet
Dim today As Date, startOfWeek As Date, endOfWeek As Date
Set ws = ActiveSheet
today = DateValue(Now)
startOfWeek = today - (Weekday(today) - 1) ' Sunday–Saturday
'Debug.Print "startOfWeek: " & startOfWeek
endOfWeek = startOfWeek + 6
Call HideColumnsOutsideRange(ws, startOfWeek, endOfWeek)
End Sub