Public Function CountWeekDays(ByVal StartDate As Variant, ByVal EndDate As Variant) As Variant
Dim lDaysTotal As Long
Dim nPartWeekStart As Integer
Dim nPartWeekEnd As Integer
Dim vTemp as Variant
'Make sure we got dates!
If VarType(StartDate) <> vbDate Or _
VarType(EndDate) <> vbDate Then Exit Function
'Make sure we didn't get Nulls
If IsNull(StartDate) Or IsNull(EndDate) Then Exit Function
'Swap dates round if backwards
If StartDate > EndDate Then
vTemp = StartDate
StartDate = EndDate
EndDate = vTemp
End If
'Work out total number of days inclusive of start and end date
lDaysTotal = EndDate - StartDate + 1
'Adjust for part week at start of period
nPartWeekStart = IIf(Weekday(StartDate) <> 1, 7 - Weekday(StartDate), 0)
If nPartWeekStart > 0 Then
StartDate = StartDate + (8 - Weekday(StartDate))
End If
'Adjust for part week at end of period
nPartWeekEnd = IIf(Weekday(EndDate) <> 7, Weekday(EndDate) - 1, 0)
If nPartWeekEnd > 0 Then
EndDate = EndDate - Weekday(EndDate)
End If
'Working days is calculated as 5 days for each week between adjusted StartDate
'and adjusted EndDate, then adding the part week bits.
'Note the use of integer division.
lDaysTotal = EndDate - StartDate + 1
CountWeekDays = 5 * (lDaysTotal \ 7) + nPartWeekStart + nPartWeekEnd
End Function