Function NthXDay(pdate As Variant, _
pWDay As Integer, _
pIncrement As Integer) As Date
'***************************************************************
'Purpose: Some holidays fall on the Nth XDay of the month.
' Given a month, year, weekday and increment (e.g.
' 1st, 3rd, last) this function returns the specific
' date.
'Coded by: raskew
'Inputs: ? NthXDay(dateserial(year(date), 11, 1),vbMonday, 3)
'Note: Use 6 to indicate the last increment, since no month
' will have 6 of any specified weekday.
'***************************************************************
Dim dteDate As Date
Dim newDate As Date
dteDate = DateValue(pdate)
'adjust the increment if it's more than 6
pIncrement = IIf(pIncrement > 6, 6, pIncrement)
'determine first day of given month
dteDate = DateSerial(Year(dteDate), Month(dteDate), 1)
'determine first specified day of given month, e.g. vbSunday
newDate = dteDate - WeekDay(dteDate) + pWDay + IIf(WeekDay(dteDate) > pWDay, 7, 0)
'determine the nth specified day of given month
newDate = DateAdd("d", 7 * (pIncrement - 1), newDate)
'if the resulting calculation is greater than the length of the
'specified month, cycle backwards to the last specified day of
'the month
Do While Month(newDate) <> Month(dteDate)
newDate = newDate - 7
Loop
NthXDay = newDate
End Function