Can you give these a go.
Are you happy to concert the rates data using the code that I supplied earlier.
It would make calculating the costs much easier.
Success depends on the data that you are given adhering to certain rules.
A check could be made that it does before you process it.
The time of 00:00:00 causes a problem so needs to be replaced with 23:59:59.
Some VBA code can be written to do this.
Column L - 00:00 to 07:00
=fncGetHoursWorked(IF($G2<$E2,TIME(0,0,0),$E2),$G2, TIME(0, 0, 0), TIME(7, 0, 0),FALSE)
Column M - 07:00 to 20:00
=fncGetHoursWorked($E2,IF($D2<>$F2,TIME(20,0,0),$G2), TIME(7, 0, 0), TIME(20, 0, 0),FALSE)
Column N - 20:00 to 23:59:59
=fncGetHoursWorked($E2,IF($G2<$E2,TIME(23, 59,59),$G2), TIME(20, 0, 0), TIME(23, 59,59),FALSE)
The fncGetHoursWorked function that us called from the three columns is in the code below.
Copy and paste this into a standard code module before you copy and paste the above formulas into the worksheet.
VBA Code:
Public Function fncGetHoursWorked(dteStartTime As Date, dteEndTime As Date, dteShiftStart As Date, dteShiftEnd As Date, blnEarlier As Boolean)
Dim dblHours As Double
If Not (dteStartTime > dteShiftEnd Or dteEndTime < dteShiftStart) Then
dblHours = (Application.WorksheetFunction.Min(dteEndTime, dteShiftEnd) - Application.WorksheetFunction.Max(dteStartTime, dteShiftStart))
End If
fncGetHoursWorked = Application.WorksheetFunction.Ceiling(dblHours * 24, 0.01)
End Function