I'm making a customised roster worksheet to schedule employees' shifts. There are days when employees have to work on split shifts. I was surfing the web and found this idea in the following link quite impressive.
https://www.findmyshift.com/au/free-excel-roster-template
This has vba code on it (which is locked ofcoz) which returns the cell value "8am-2pm" to "6" (means calculate the time difference given in text format).
For split shifts, cell value "8am-2pm/4pm-9pm" will return "11".
Can someone please be able to provide me a code to achieve similar results.
Thanks in advance.
Here's your function. It can handle any number of shifts, with hours or hours and minutes and spanning across midnight like 8:30pm-2:45am
:
Public Function TimeDifference(s As String) As Variant
On Error GoTo Handler
Dim shifts() As String
Dim result As Double
Dim hours() As String
Dim duration As Double
result = 0
shifts = Split(s, "/")
For Each shift In shifts
hours = Split(shift, "-")
duration = TimeValue(hours(1)) - TimeValue(hours(0))
If duration < 0 Then
duration = duration + 1
End If
result = result + duration * 24
Next shift
TimeDifference = result
Exit Function
Handler:
TimeDifference = CVErr(xlErrValue)
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.