I'm dealing with times in Excel with no date. The times refer to the start and end of a workers duty.
I'm trying to find the different between the 2 times to ensure a worker has enough time between 2 duties.
The results of this gives me
I also want to be able to subtract times from midnight, so for example if they start at
I had tried adding times that cross midnight (or from midnight) and those additions appear to work as expected.
I use a VLOOKUP to find the corresponding start and end time of a duty code in a duty table. The roster is in a separate range of workers names as rows and columns as the day of the month with the intersecting cells populated with a duty code.
I'm trying to find the different between the 2 times to ensure a worker has enough time between 2 duties.
VBA Code:
Sub test_time_diff()
Dim start_time As Date
Dim end_time As Date
today_duty_value = "F"
yesterday_duty_value = "V"
'column 2 is the start time and column 3 is the end time of duties
start_time = Format(Format(Application.VLookup(today_duty_value, range("duty_times"), 2, False), "hh:mm"))
'start_time is now 06:30:00
end_time = Format(Format(Application.VLookup(yesterday_duty_value, range("duty_times"), 3, False), "hh:mm"))
'end_time is now 22:30:00
Debug.Print Format(start_time - end_time, "hh:mm")
End Sub
The results of this gives me
16:00
when in fact it's only 08:00
.I also want to be able to subtract times from midnight, so for example if they start at
00:00
and I subtract 2 hours from midnight I want the result to be 22:00
I had tried adding times that cross midnight (or from midnight) and those additions appear to work as expected.
I use a VLOOKUP to find the corresponding start and end time of a duty code in a duty table. The roster is in a separate range of workers names as rows and columns as the day of the month with the intersecting cells populated with a duty code.