If I understand, you need a function to return the
Week Number, given a date. Try this code:
Code:Public Function WeekNumber(InDate As Date) As Long Dim DayNo As Long Dim StartDays As Long Dim StopDays As Long Dim StartDay As Long Dim StopDay As Long Dim VNumber As Long Dim ThurFlag As Boolean DayNo = Days(InDate) StartDay = Weekday(DateSerial(Year(InDate), 1, 1)) - 1 StopDay = Weekday(DateSerial(Year(InDate), 12, 31)) - 1 ' Number of days belonging to first calendar week StartDays = 7 - (StartDay - 1) ' Number of days belonging to last calendar week StopDays = 7 - (StopDay - 1) ' Test to see if the year will have 53 weeks or not ThurFlag = (StartDay = 4) Or (StopDay = 4) VNumber = (DayNo - StartDays - 4) / 7 ' If first week has 4 or more days, it will be calendar week 1 ' If first week has less than 4 days, it will belong to last year's ' last calendar week If StartDays >= 4 Then WeekNumber = Fix(VNumber) + 2 Else WeekNumber = Fix(VNumber) + 1 End If ' Handle years whose last days will belong to coming year's first ' calendar week If WeekNumber > 52 And ThurFlag = False Then WeekNumber = 1 ' Handle years whose first days will belong to the last year's ' last calendar week If WeekNumber = 0 Then WeekNumber = WeekNumber(DateSerial(Year(InDate) - 1, 12, 31)) End If End Function Private Function Days(DayNo As Date) As Long Days = DayNo - DateSerial(Year(DayNo), 1, 0) End Function Function DaysInMonth(YearValue As Long, MonthValue As Long) As Long DaysInMonth = Day(DateSerial(YearValue, MonthValue + 1, 0)) End Function 'sample useage Private Sub Form_Load() Dim i As Long Dim d As Date For i = 1 To 31 d = DateSerial(2011, 5, i) Debug.Print d, WeekNumber(d) Next End Sub




Reply With Quote