Public Function getShipWeek(dtmShipDate As Date, shippingYear As Integer) As Integer
Dim lastDayOfYear As Date
lastDayOfYear = DateSerial(shippingYear, 12, 31)
'verify the shipping date is in the current year or the next year
If Year(dtmShipDate) < shippingYear Then
Exit Function
End If
'If it is in the next year verify if falls in the last shipping week
If dtmShipDate > lastDayOfYear Then
If inLastWeek(dtmShipDate, shippingYear) Then
getShipWeek = getShipWeek(lastDayOfYear, shippingYear)
End If
Else
getShipWeek = DatePart("ww", dtmShipDate, vbMonday, vbFirstJan1)
End If
End Function
Public Function inLastWeek(dtmShipDate As Date, shippingYear As Integer) As Boolean
Dim lastDayOfYear As Date
Dim tempDate As Date
lastDayOfYear = DateSerial(shippingYear, 12, 31)
tempDate = lastDayOfYear
Do
tempDate = tempDate + 1
If dtmShipDate = tempDate Then
inLastWeek = True
Exit Function
End If
Loop Until Weekday(tempDate) = vbSunday
End Function
[code]
Verification
Building the 2009 calendar
?getShipweek(#1/1/2009#,2009)
1
?getShipweek(#1/1/2010#,2009)
53
building the 2010 calendar
?getShipweek(#1/1/2010#,2010)
1
Error checking. Passing a 2009 date when building the 2011 calendar
?getShipweek(#1/1/2009#,2011)
0
You may also want to play with the DatePartfunction. This assumes the first week of the year start wherever Jan 1 falls. But if Jan 1 falls on a Sunday and a week goes Monday to Sunday would you want week 1 to be that one day week or the next week?