Day Of Week Functions
This page describes a number of formulas and VBA functions you can use when
working with days of the week.
Many, if not most, workbooks work with dates in one fashion or another. This page describes
9 functions, implemented both as worksheet formulas and as VBA functions, that you can use
when working with days of the week.
In all functions, the day of the week is expressed as a number, where 1 = Sunday, 2 = Monday, ...,
7 = Saturday. It should also be noted that most of the functions use a modulus operation, Excel's
MOD worksheet function. The MOD worksheet function
and the VBA Mod operator can produce different results, specifically when negative
numbers are involved. In order to maintain logical continuity between the worksheet formulas presented here
and their VBA function equivalents, the VBA code uses a function named WSMod that
behaves the same as the MOD worksheet function. If you write your own VBA functions
based on the code provided here, you will want to use the WSMod function rather than
VBA's Mod operator.
The following formulas and VBA functions are described on this page and are available in the download files.
- DaysOfWeekBetweenTwoDates
This returns the number of Day Of Week days between two dates.
For example, the number of Tuesdays between 15-Jan-2009 and 26-July-2010.
- DaysOfWeekInMonth This returns the number of a given Day Of Week in a given month and year.
For example, the number of Tuesdays in April, 2009.
- DateOfPreviousDayOfWeek
This returns the date of the first Day Of Week before a given date.
For example, the date of the Tuesday before 15-June-2009.
- DateOfNextDayOfWeek
This returns the date of the first Day Of Week following a given date. For
example, the date of the first Tuesday after 15-June-2009.
- FirstDayOfWeekInMonth
This returns the date of the first Day Of Week day in a given month and year.
For example, the date of the first Friday in March, 2010.
- LastDayOfWeekInMonth
This returns the date of the last Day Of Week day in a given month and year.
For exampe, the date of the last Friday in May, 2009.
- NthDayOfWeekInMonth
This returns the date of the Nth Day Of Week day in a given month and year.
For example, the date of the third Friday in May, 2009.
- FirstDayOfWeekOfYear
This returns the date of the first Day Of Week day of a given year.
For example, the date of the first Friday in 2009.
- LastDayOfWeekOfYear
This returns the date of the last Day Of Week day of a given year. For example,
the date of the last Monday in 2009.
These functions, in both worksheet formula and VBA implementations, are described below. The WSMod function, which is
used in place of VBA's Mod operator, is as follows:
Function WSMod(Number As Double, Divisor As Double) As Double
WSMod = Number - Divisor * Int(Number / Divisor)
End Function
This returns the number of Day Of Week days between two dates. For example, the number of Tuesdays between 6-Jan-2009 and 31-Jan-2009 is 4.
=((EndDate-MOD(WEEKDAY(EndDate)-DayOfWeek,7)-
StartDate-MOD(DayOfWeek-WEEKDAY(StartDate)+7,7))/7)+1
In VBA,
Public Function DaysOfWeekBetweenTwoDates(StartDate As Date, _
EndDate As Date, DayOfWeek As VbDayOfWeek) As Variant
If StartDate > EndDate Then
DaysOfWeekBetweenTwoDates = CVErr(xlErrNum)
Exit Function
End If
If (DayOfWeek < vbSunday) Or (DayOfWeek > vbSaturday) Then
DaysOfWeekBetweenTwoDates = CVErr(xlErrValue)
Exit Function
End If
If (StartDate < 0) Or (EndDate < 0) Then
DaysOfWeekBetweenTwoDates = CVErr(xlErrValue)
Exit Function
End If
DaysOfWeekBetweenTwoDates = _
((EndDate - WSMod(Weekday(EndDate) - DayOfWeek, 7) - StartDate - _
WSMod(DayOfWeek - Weekday(StartDate) + 7, 7)) / 7) + 1
End Function
This returns the number of Day Of Week days in a given month and year. For example, the number of Sundays in January, 2009, is 5.
=((DATE(YYear,MMonth+1,0)-MOD(WEEKDAY(DATE(YYear,MMonth+1,0))-DayOfWeek,7)-
DATE(YYear,MMonth,1)-MOD(DayOfWeek-WEEKDAY(DATE(YYear,MMonth,1))+7,7))/7)+1
In VBA,
Public Function DaysOfWeekInMonth(MMonth As Long, YYear As Long, _
DayOfWeek As VbDayOfWeek) As Variant
If (MMonth < 1) Or (MMonth > 12) Then
DaysOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
If (YYear < 1900) Or (YYear > 9999) Then
DaysOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
If (DayOfWeek < vbSunday) Or (DayOfWeek > vbSaturday) Then
DaysOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
DaysOfWeekInMonth = ((DateSerial(YYear, MMonth + 1, 0) - _
WSMod(Weekday(DateSerial(YYear, MMonth + 1, 0)) - DayOfWeek, 7) - _
DateSerial(YYear, MMonth, 1) - WSMod(DayOfWeek - _
Weekday(DateSerial(YYear, MMonth, 1)) + 7, 7)) / 7) + 1
End Function
This function returns the date of the first Day Of Week day prior to a given date. For example, the Tuesday prior to 31-Jan-2009 is 27-Jan-2009.
=StartDate-MOD(WEEKDAY(StartDate)-DayOfWeek,7)
In VBA,
Public Function PreviousDayOfWeek(StartDate As Date, _
DayOfWeek As VbDayOfWeek) As Variant
If (DayOfWeek < vbSunday) Or (DayOfWeek > vbSaturday) Then
PreviousDayOfWeek = CVErr(xlErrValue)
Exit Function
End If
If (StartDate < 0) Then
PreviousDayOfWeek = CVErr(xlErrValue)
Exit Function
End If
PreviousDayOfWeek = StartDate - WSMod(Weekday(StartDate) - DayOfWeek, 7)
End Function
This returns the date of the first Day Of Week day following a given date. The Sunday following 15-Jan-2009 is 18-Jan-2009.
=StartDate+MOD(DayOfWeek-WEEKDAY(StartDate),7)
In VBA,
Public Function NextDayOfWeek(StartDate As Date, _
DayOfWeek As VbDayOfWeek) As Variant
If (DayOfWeek < vbSunday) Or (DayOfWeek > vbSaturday) Then
NextDayOfWeek = CVErr(xlErrValue)
Exit Function
End If
If (StartDate < 0) Then
NextDayOfWeek = CVErr(xlErrValue)
Exit Function
End If
NextDayOfWeek = StartDate + WSMod(DayOfWeek - Weekday(StartDate), 7)
End Function
This returns then first Day Of Week in a given month and year. For example, the first Sunday in June, 2009, is 7-June-2009.
=DATE(YYear,MMonth,1)+(MOD(DayOfWeek-WEEKDAY(DATE(YYear,MMonth,1)),7))
In VBA,
Public Function FirstDayOfWeekInMonth(MMonth As Long, YYear As Long, _
DayOfWeek As VbDayOfWeek) As Variant
If (DayOfWeek < vbSunday) Or (DayOfWeek > vbSaturday) Then
FirstDayOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
If (MMonth < 1) Or (MMonth > 12) Then
FirstDayOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
FirstDayOfWeekInMonth = DateSerial(YYear, MMonth, 1) + _
WSMod(DayOfWeek - Weekday(DateSerial(YYear, MMonth, 1)), 7)
End Function
This returns the last Day Of Week day in a given month and year. For example, the last Wednesday in November, 2009, is 28-November-2009.
=DATE(YYear,MMonth+1,0)-ABS(WEEKDAY(DATE(YYear,MMonth+1,0))-DayOfWeek)
In VBA,
Public Function LastDayOfWeekInMonth(MMonth As Long, YYear As Long, _
DayOfWeek As VbDayOfWeek) As Variant
If (DayOfWeek < vbSunday) Or (DayOfWeek > vbSaturday) Then
LastDayOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
LastDayOfWeekInMonth = DateSerial(YYear, MMonth + 1, 0) - _
Abs(Weekday(DateSerial(YYear, MMonth + 1, 0)) - DayOfWeek)
End Function
This returns the Nth Day Of Week day in a given month and year. For example, the third Thursday of September, 2009, is 17-Sept-2009.
=DATE(YYear,MMonth,1)+(MOD(DayOfWeek-WEEKDAY(DATE(YYear,MMonth,1)),7))+(7*(Nth-1))
In VBA,
Public Function NthDayOfWeekInMonth(MMonth As Long, YYear As Long, _
DayOfWeek As VbDayOfWeek, Nth As Long) As Variant
If (MMonth < 1) Or (MMonth > 12) Then
NthDayOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
If (YYear < 1900) Or (YYear > 9999) Then
NthDayOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
If (DayOfWeek < vbSunday) Or (DayOfWeek > vbSaturday) Then
NthDayOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
If Nth < 0 Then
NthDayOfWeekInMonth = CVErr(xlErrValue)
Exit Function
End If
NthDayOfWeekInMonth = DateSerial(YYear, MMonth, 1) + _
(WSMod(DayOfWeek - Weekday(DateSerial(YYear, MMonth, 1)), 7)) + _
(7 * (Nth - 1))
End Function
This returns the date of the first Day Of Week day in a given year. For example, the first Tuesday in 2009 is 6-Jan-2009.
=DATE(YYear,1,1)+MOD(DayOfWeek-WEEKDAY(DATE(YYear,1,1)),7)
In VBA,
Public Function FirstDayOfWeekOfYear(YYear As Long, DayOfWeek As VbDayOfWeek) As Variant
If (YYear < 1900) Or (YYear > 9999) Then
FirstDayOfWeekOfYear = CVErr(xlErrValue)
Exit Function
End If
If (DayOfWeek < vbSunday) Or (DayOfWeek > vbSaturday) Then
FirstDayOfWeekOfYear = CVErr(xlErrValue)
Exit Function
End If
FirstDayOfWeekOfYear = DateSerial(YYear, 1, 1) + _
WSMod(DayOfWeek - Weekday(DateSerial(YYear, 1, 1)), 7)
End Function
This returns the date of the last Day Of Week day in a given year. For example, the last Wednesday in 2009 is 30-Dec-2009.
=DATE(YYear,12,31)-MOD(WEEKDAY(DATE(YYear,12,31))-DayOfWeek,7)
In VBA,
Public Function LastDayOfWeekOfYear(YYear As Long, DayOfWeek As VbDayOfWeek) As Variant
If (YYear < 1900) Or (YYear > 9999) Then
LastDayOfWeekOfYear = CVErr(xlErrValue)
Exit Function
End If
If (DayOfWeek < vbSunday) Or (DayOfWeek > vbSaturday) Then
LastDayOfWeekOfYear = CVErr(xlErrValue)
Exit Function
End If
LastDayOfWeekOfYear = DateSerial(YYear, 12, 31) - _
WSMod(Weekday(DateSerial(YYear, 12, 31)) - DayOfWeek, 7)
End Function
|
This page last updated: 15-August-2009. |