Skip to content

Instantly share code, notes, and snippets.

@esycat
Created July 14, 2019 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esycat/b49c3d0030f59aeb5a61d127fab337df to your computer and use it in GitHub Desktop.
Save esycat/b49c3d0030f59aeb5a61d127fab337df to your computer and use it in GitHub Desktop.
A custom function for Excel to find Nth weekday of the month
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