Function WeekOfMonth(thedate As Date)
Dim firstofmonth As Byte, offset As Byte
Dim strdate As String
strdate = Month(thedate) & "/1/" & Year(thedate)
firstofmonth = Weekday(CDate(strdate))
offset = 9 - firstofmonth
WeekOfMonth = Switch(Day(thedate) < offset, 1, _
Day(thedate) < (offset + 7), 2, _
Day(thedate) < (offset + 14), 3, _
Day(thedate) < (offset + 21), 4, _
Day(thedate) < (offset + 28), 5, True, 6)
MsgBox WeekOfMonth
[COLOR=green] ' strdate plugs in "day 1" of the selected month and year
' firstofmonth uses the Weekday function to determine
' a numeric value for day 1
' if Sunday, firstofmonth = 1
' if Monday, firstofmonth = 2
' etc.
' if Saturday, firstofmonth = 7
' the offset value = 9 - firstofmonth
' for example, if firstofmonth = 1 (Sunday)
' then week 1 will include days 1 - 7,
' or all days < (9 - 1)
'
' if firstofmonth = 7 (Saturday)
' then week 1 will include day 1 only,
' which in this case is the only day < (9 - 7)
' instead of hard-coding the days 8, 15, 22, 29,
' this method will take the "offset" value for week 1,
' and just increment it by 7 for each subsequent week.
' This even works for months with 6 partial weeks -
' that is why the "True" portion (the else) of the Switch
' function is set to 6, not 5.
'
' (Test this with 11/1/2003 and 11/30/2003 to get a good example)
' Of course if you don't want to use Sunday as the first day of the
' week, you can adjust the argument in the Weekday function. [/color]
End Function