Week Numbers In Excel
This page describes how to work with week numbers in Excel.
Many applications and organizations use the week number of a date for some sort of identification
or formatting purpose. However, what constitutes a week number, specifically when does Week 1 begin,
is not always the same from one organization or application to another. This page describes worksheet
formulas and VBA procedures that you can use to calculate a week number in a number of different manners.
The formulas and functions described on this page work with the following definitions of the first day of week 1 of a year:
- The first day of Week 1 is January of that year, regardless of what day of the week that might be.
- The first day of Week 1 is the first occurrence of some specified day of the week. For example, you may define
Week 1 to begin on the first Monday of the year.
- The first day of Week 1 is a specific date. For example, you may define that Week 1 always begins on May 1 of the year.
- The first day of Week 1 is the first occurrence of one day of the week following the first occurrence of another day of
the week. For example, you may define that Week 1 begins on the first Monday after the first Thursday of the year.
- The first day of Week 1 is specified by the Internation Standards Organization (ISO) definiton.

This is the simplest method. It assumes that Week 1 begins on January 1, regardless of what day of the week that might be. The
formula for this is:
=TRUNC(((StartDate-DATE(YEAR(StartDate),1,0))+6)/7)
where StartDate is the date whose week number is to be returned.
The VBA code for this function is:
Public Function WeekNumberAbsolute(DT As Date) As Long
WeekNumberAbsolute = Int(((DT - DateSerial(Year(DT), 1, 0)) + 6) / 7)
End Function

This method uses as the first day of Week 1 the first occurrence of a specified day of the week. For example, you might define
that Week 1 begins on the first Monday of the year. The formula is:
=TRUNC(((StartDate-DATE(YEAR(StartDate),1,1)+
MOD(DayOfWeek-WEEKDAY(DATE(YEAR(StartDate),1,1)),7))+6)/7)
where StartDate is the whose week number is to be found, and DayOfWeek is the
day of week number (1 = Sunday, 2 = Monday, ..., 7 = Saturday).
The VBA code for this function is:
Public Function WeekNumberFromFirstDayOfWeek(DT As Date, DayOfWeek As VbDayOfWeek) As Long
WeekNumberFromFirstDayOfWeek = Int(((DT - DateSerial(Year(DT), 1, 1) + _
WSMod(DayOfWeek - Weekday(DateSerial(Year(DT), 1, 1)), 7)) + 6) / 7)
End Function

This method uses a specific date as the first day of Week 1. The formula is:
=TRUNC(((TestDate-StartDate)+6)/7)+(WEEKDAY(TestDate)=WEEKDAY(StartDate))
where TestDate is the date whose week number is to be calcuatled, and StartDate
specifies the first day of Week 1.
The VBA code for this function is:
Public Function WeekNumberFromDate(DT As Date, StartDate As Date) As Long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' WeekNumberFromDate
' This returns the week number base on Week 1 starting on StartDate.
' =TRUNC(((DT-StartDate)+6)/7)+(WEEKDAY(DT)=WEEKDAY(StartDate))
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
WeekNumberFromDate = Int(((DT - StartDate) + 6) / 7) + Abs(Weekday(DT) = Weekday(StartDate))
End Function

This methods uses as the first day of Week 1 the first occurrence of one specified day of the week that follows the first occurrence
of another day of the week. For example, you might want the first day of week to be the Monday following the first Thursday of the year.
The formula is:
=TRUNC(((TestDate-((DATE(YEAR(TestDate),1,1)+
MOD(BaseDayOfWeek-WEEKDAY(DATE(YEAR(TestDate),1,1)),7))+
(MOD(StartDayOfWeek-WEEKDAY((DATE(YEAR(TestDate),1,1)+
MOD(BaseDayOfWeek-WEEKDAY(DATE(YEAR(TestDate),1,1)),7))),7))))+6)/7)+
(WEEKDAY(TestDate)=WEEKDAY(((DATE(YEAR(TestDate),1,1)+
MOD(BaseDayOfWeek-WEEKDAY(DATE(YEAR(TestDate),1,1)),7))+
(MOD(StartDayOfWeek-WEEKDAY((DATE(YEAR(TestDate),1,1)+
MOD(BaseDayOfWeek-WEEKDAY(DATE(YEAR(TestDate),1,1)),7))),7)))))
where TestDate is the date whose week number is to be calculated. StartDayOfWeek is the
number of the day of the week after which BaseDate is to be found. BaseDayOfWeek is
the weekday number of the day following the first occurrence of BaseDayOfWeek that week 1 should begin. For example,
to use the first Monday following the first Thursday of the year, BaseDayOfWeek should be 2
and StartDate should be 5.
The VBA code for this function is:
Public Function WeekNumberFromDayOfWeekDay(DT As Date, BaseDayOfWeek As VbDayOfWeek, _
StartDayOfWeek As VbDayOfWeek) As Long
Dim FirstDayOfYear As Long
Dim FirstDayOfYearWeekday As Long
FirstDayOfYear = DateSerial(Year(DT), 1, 1)
FirstDayOfYearWeekday = Weekday(FirstDayOfYear)
WeekNumberFromDayOfWeekDay = Int(((DT - ((FirstDayOfYear + _
WSMod(BaseDayOfWeek - FirstDayOfYearWeekday, 7)) + _
(WSMod(StartDayOfWeek - Weekday((FirstDayOfYear + _
WSMod(BaseDayOfWeek - FirstDayOfYearWeekday, 7))), 7)))) + 6) / 7) + _
(Weekday(DT) = Weekday(((FirstDayOfYear + WSMod(BaseDayOfWeek - _
FirstDayOfYearWeekday, 7)) + _
(WSMod(StartDayOfWeek - Weekday((FirstDayOfYear + _
WSMod(BaseDayOfWeek - FirstDayOfYearWeekday, 7))), 7)))))
End Function

This method uses the International Standards Organization (ISO) definition of a week number. The formula is:
=INT((StartDate-DATE(YEAR(StartDate-WEEKDAY(StartDate-1)+4),1,3)+
WEEKDAY(DATE(YEAR(StartDate-WEEKDAY(StartDate-1)+4),1,3))+5)/7)
where StartDate is the date whose week number is to be calculated.
The VBA code for this function is:
Public Function WeekNumberISO(InDate As Date) As Long
Dim D As Date
D = DateSerial(Year(InDate - Weekday(InDate - 1) + 4), 1, 3)
WeekNumberISO = Int((InDate - D + Weekday(D) + 5) / 7)
End Function

The VBA Mod operator and the MOD worksheet function do not always return the
same result, particularly when negative numbers are involved. To maintain the continuity between the VBA functions and their
worksheet formula counterparts, the VBA functions us a function named WSMod that works as does the
Excel MOD function. WSMod is required by many of the functions on this page
and you should use it, rather than VBA's Mod operator if you modify these functions.
The VBA code for this function is:
Private Function WSMod(Number As Double, Divisor As Double) As Double
WSMod = Number - Divisor * Int(Number / Divisor)
End Function
 |
This page last updated: 3-September-2009. |