Public Function WeekdayHours(dtStart As Date, dtEnd As Date, _
dtDayStart As Date, dtDayEnd As Date) As Double
'dtStart is the starting date with time
'dtEnd is the ending date with time
'dtDayStart is the time work starts
'dtDayEnd is the time work ends
Dim dblDuration As Double
Dim dtDayDuration As Date
Dim dtTimeStart As Date
Dim dtTimeEnd As Date
dtTimeStart = TimeValue(dtStart)
dtTimeEnd = TimeValue(dtEnd)
If dtTimeStart < dtDayStart Then
dtTimeStart = dtDayStart
End If
If dtTimeEnd > dtDayEnd Then
dtTimeEnd = dtDayEnd
End If
Dim dtThis As Date 'used in loop
'how long is a single day of work
dtDayDuration = dtDayEnd - dtDayStart
If DateValue(dtStart) = DateValue(dtEnd) Then
If Weekday(dtStart) Mod 6 <> 1 Then
dblDuration = dtTimeEnd - dtTimeStart
End If
Else
If Weekday(dtStart) Mod 6 <> 1 Then
dblDuration = 1 - TimeValue(dtStart)
End If
For dtThis = dtStart + 1 To dtEnd - 1
If Weekday(dtThis) Mod 6 <> 1 Then
dblDuration = dblDuration + dtDayDuration
End If
Next
If Weekday(dtEnd) Mod 6 <> 1 Then
dblDuration = dblDuration + dtTimeEnd - dtDayStart
End If
End If
WeekdayHours = dblDuration * 24
End Function