Spriglet #9: Conditionally hide columns in Excel using VB

I have time tracking worksheet that I use at work. It looks something like this:

Task Total Hours Friday, 9/7/2007 Saturday, 9/8/2007
Status Report 5.25 1.5 0
Staff Meeting 12.5 1.5 0

Since I don’t work on Saturday or Sunday, I’d like to hide all columns that have a date that’s a Saturday or Sunday.

Here’s a little VB macro that will do the trick:

Sub header()

Dim eCol As Integer
Dim i As Integer

eCol = Cells(1, Columns.Count).End(xlToLeft).Column

For i = 1 To eCol

With Cells(1, i)

If IsDate(.Value) Then
If (Weekday(.Value) = vbSunday) Or _
(Weekday(.Value) = vbSaturday) Then
.EntireColumn.Hidden = True
End If
End If

End With

Next i

End Sub

h/t Rowan

Wow. It's Quiet Here...

Be the first to start the conversation!

Leave a Reply:

Gravatar Image

Switch to mobile version