I have a set of dates from which I must obtain the week of the month.
There is a lot of literature on how to obtain the week of the year using VBA code but not the week number of the month.
For instance 03-Mar-13 would give week 1 of March, instead I end up with a result of week 10.
This isn't the most elegant code, but it worked for me.
Some assumptions:
=====
Function WeekOfMonth(selDate As Date)
Dim DayOfFirst As Integer
Dim StartOfWeek2 As Integer
Dim weekNum As Integer
DayOfFirst = Weekday(DateSerial(Year(selDate), Month(selDate), 1), vbSunday)
StartOfWeek2 = (7 - DayOfFirst) + 2
Select Case selDate
Case DateSerial(Year(selDate), Month(selDate), 1) _
To DateSerial(Year(selDate), Month(selDate), StartOfWeek2 - 1)
weekNum = 1
Case DateSerial(Year(selDate), Month(selDate), StartOfWeek2) _
To DateSerial(Year(selDate), Month(selDate), StartOfWeek2 + 6)
weekNum = 2
Case DateSerial(Year(selDate), Month(selDate), StartOfWeek2 + 7) _
To DateSerial(Year(selDate), Month(selDate), StartOfWeek2 + 13)
weekNum = 3
Case DateSerial(Year(selDate), Month(selDate), StartOfWeek2 + 14) _
To DateSerial(Year(selDate), Month(selDate), StartOfWeek2 + 20)
weekNum = 4
Case DateSerial(Year(selDate), Month(selDate), StartOfWeek2 + 21) _
To DateSerial(Year(selDate), Month(selDate), StartOfWeek2 + 27)
weekNum = 5
Case DateSerial(Year(selDate), Month(selDate), StartOfWeek2 + 28) _
To DateSerial(Year(selDate), Month(selDate) + 1, 1)
weekNum = 6
End Select
WeekOfMonth = weekNum
End Function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With