Heres my code:
Function ElapsedTime(StartTime, EndTime, StartDay, EndDay, Format As String) As String
'Set Numerical Elapsed Time to Zero
Dim ElapsedTimeN As Double
ElapsedTimeN = 0
DayCount = Int(EndTime) - Int(StartTime)
DayLength = (EndDay - StartDay) * 24
'Calculate Mins on 1st Day
'Check it's a workday
If Weekday(Int(StartTime), vbMonday) < 6 Then
'If it's open and closed in a day take the closed time else take the End of Day time
If Int(EndTime) = Int(StartTime) Then CloseTime = EndTime Else CloseTime = Int(StartTime) + EndDay
If StartTime > CloseTime Then ElapsedTimeN = 0 Else ElapsedTimeN = ElapsedTimeN + ((CloseTime - StartTime) * 24)
End If
'Calculate Mins on Last Day
'Check It's a workday
If Weekday(Int(EndTime), vbMonday) < 6 Then
'If it's open and closed on the same day do nothing
If (Int(EndTime) = Int(StartTime)) Or (Int(EndTime) + StartDay > EndTime) Then
Else
'Otherwise add the hours until Endtime to the Duration
ElapsedTimeN = ElapsedTimeN + (EndTime - Int(EndTime)) * 24
End If
End If
'Check for a span of more that two days
If DayCount > 1 Then
'Start on the second day and run to the penultimate day
For TestDay = Int(StartTime) + 1 To Int(EndTime) - 1
If Weekday(Int(TestDay), vbMonday) < 6 Then
ElapsedTimeN = ElapsedTimeN + DayLength
End If
Next TestDay
End If
DayCount = Int(ElapsedTimeN / DayLength)
HourCount = Int(ElapsedTimeN - (DayCount * DayLength))
MinsCount = Int((ElapsedTimeN - Int(ElapsedTimeN)) * 60)
If Format = "d" Then TextOut = DayCount & "Days " & HourCount & "Hours " & MinsCount & "Mins"
If Format = "h" Then TextOut = (DayCount * DayLength) + HourCount & "Hours " & MinsCount & "Mins"
If Format = "m" Then TextOut = ((DayCount * DayLength) + HourCount) * 60 + MinsCount & "Mins"
ElapsedTime = TextOut
End Function