XLA routines: EE_NthWeekdayOfMonth

Nick's picture
EE_NthWeekdayOfMonth returns the nth weekday of the month that a date is in. - so you could use this function to find the 3rd wednesday in the month (an IMM date for example), or the 2nd Friday... whatever you need
Function EE_NthWeekdayOfMonth(dt As Date, nthWkday As Integer, wkday As EE_Weekday) As Date
'Takes a date, n, and the day of the week (mon, tue, wed) and returns the 2nd tuesday (for example) of the month.
    Dim x       As Integer
    Dim dtLoop  As Date
 
'http://excelexperts.com/xla-routines-eeNthWeekdayOfMonth    for updates on this function

    For dtLoop = EE_FirstDayOfMonth(dt) To EE_LastDayOfMonth(dt)
        If Weekday(dtLoop) = wkday Then
            x = x + 1
            If x = nthWkday Then
                EE_NthWeekdayOfMonth = dtLoop
                Exit Function
            End If
        End If
    Next dtLoop
End Function