Public Const p1 = #7:00:00 AM#
Public Const p2 = #11:30:00 AM#
Public Const p3 = #1:30:00 PM#
Public Const p4 = #5:00:00 PM#
Function NWTIMES(ByVal StartDate As Long, StartTime As Double, _
ByVal EndDate As Long, ByVal EndTime As Double, _
Optional ByVal IgnoreSunday As Boolean = False) As Double
Dim t1 As Double, tA As Double, tB As Double, dTotal As Double
Dim dstTime As Double, dndTime As Double
Dim lstDate As Long, lndDate As Long, tmpDays As Long, lSuns As Long
dstTime = StartTime: dndTime = EndTime
lstDate = StartDate: lndDate = EndDate
If IgnoreSunday Then
If Weekday(lstDate) = 1 Then
lstDate = lstDate + 1
dstTime = p1
End If
If Weekday(lndDate) = 1 Then
lndDate = lndDate - 1
dndTime = p4
End If
End If
If lndDate + dndTime >= lstDate + dstTime Then
If dstTime < p1 Then dstTime = p1
If dstTime > p4 Then dstTime = p4
If dndTime < p1 Then dndTime = p1
If dndTime > p4 Then dndTime = p4
If dstTime <= p2 And dndTime >= p3 Then
t1 = dndTime - p3 + p2 - dstTime
ElseIf (dstTime <= p2 And dndTime <= p2) Or (dstTime >= p3 And dndTime >= p3) Then
t1 = dndTime - dstTime
ElseIf dstTime <= p2 And dndTime <= p3 Then
t1 = p2 - dstTime
ElseIf dstTime >= p2 And dndTime >= p3 Then
t1 = dndTime - p3
End If
If dstTime >= p3 Then
tA = p4 - dstTime
ElseIf dstTime >= p2 Then
tA = p4 - p3
Else
tA = p4 - p3 + p2 - dstTime
End If
If dndTime <= p2 Then
tB = dndTime - p1
ElseIf dndTime <= p3 Then
tB = p3 - p1
Else
tA = dndTime - p3 + p2 - p1
End If
If lndDate = lstDate Then
dTotal = t1
ElseIf lndDate - lstDate = 1 Then
dTotal = tA + tB
Else
If IgnoreSunday Then
lSuns = Int((lndDate - lstDate - Weekday(lndDate) + 8) / 7)
tmpDays = lndDate - lstDate - 1 - lSuns
Else
tmpDays = lndDate - lstDate - 1
End If
dTotal = tA + tB + tmpDays * (p4 - p3 + p2 - p1)
End If
NWTIMES = dTotal
End If
End Function