Created
July 14, 2019 12:41
-
-
Save esycat/b49c3d0030f59aeb5a61d127fab337df to your computer and use it in GitHub Desktop.
A custom function for Excel to find Nth weekday of the month
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function WEEKDAYOFMONTH(month As Date, dow As Integer, offset As Integer) | |
FirstDayOfMonth = month - Day(month) + 1 | |
LastDayOfMonth = WorksheetFunction.EoMonth(month, 0) | |
If offset > 0 Then | |
StartDate = FirstDayOfMonth + offset * 7 | |
adjustment = weekday(FirstDayOfMonth + 7 - dow) | |
ElseIf offset < 0 Then | |
StartDate = LastDayOfMonth + (offset + 1) * 7 | |
adjustment = weekday(LastDayOfMonth - dow) | |
End If | |
WEEKDAYOFMONTH = StartDate - adjustment | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment