
Originally Posted by
seņor_sriracha
I had created a method that used recursion but it was only designed to skip weekends.
Et voilā:
Code:
' recursive function to add (or subtract) business days to a date
' business days exclude Saturdays and Sundays (holidays not accounted for)
' limit 2185 days before out of stack space error
Public Function BusinessDaysFrom(ByVal dtStart As Date, days As Integer) As Date
Dim dayNum As Integer
Dim nextDay As Date
On Error GoTo bdError
' base case
If days = 0 Then
BusinessDaysFrom = dtStart
Exit Function
End If
' add (or subtract) a day to the date argument
nextDay = dtStart + IIf(days > 0, 1, -1)
dayNum = Weekday(nextDay)
' check to see if day plus (or minus) one is a saturday(7) or sunday(1)
' only decrement (or increment) the days counter if it's a weekday
If dayNum <> vbSunday And dayNum <> vbSaturday Then
days = days - IIf(days > 0, 1, -1)
End If
' call next frame
BusinessDaysFrom = BusinessDaysFrom(nextDay, days)
exitBD:
Exit Function
bdError:
MsgBox "Error calculating next business day" & vbCrLf & _
vbCrLf & Err.Description, vbCritical + vbOKOnly, "Recursion Error"
Resume exitBD
End Function