View Full Version : Adding/subtracting weekdays


raskew
03-09-2007, 07:57 AM
The following will allow the user to add/subtract weekdays (Mon - Friday) from a specified (US short-date) date. Note that holidates aren't considered.

Function UpBusDays3(pStart As Date, _
pnum As Integer, _
Optional pAdd As Boolean = True) As Date
'*******************************************
'Purpose: Add or subtract business days
' from a date
'Coded by: raskew
'Inputs: 1) +) ? UpBusDays3(#2/17/06#, 3, True)
' 2) -) ? UpBusDays3(#2/22/06#, 3, False)
'Output: 1) +) 2/22/06
' 2) -) 2/17/06
'*******************************************

Dim dteHold As Date
Dim i As Integer
Dim n As Integer

dteHold = pStart
n = pnum
For i = 1 To n
If pAdd Then 'add days
dteHold = dteHold + IIf(WeekDay(dteHold) > 5, 9 - WeekDay(dteHold), 1)
Else 'subtract days
dteHold = dteHold - IIf(WeekDay(dteHold) < 3, Choose(WeekDay(dteHold), 2, 3), 1)
End If
Next i
UpBusDays3 = dteHold

End Function


Bob