Hi,
First what I noticed is that you now have time and date not in adjacent columns but separate.
A minor modification shall be applied to show the UDF where are dates located, for instance passing this by additional argument:
Function takt_date(r_end As Range, r_date As Range, r_time As Range, r_breaks As Range) As Variant
it can be used then in calculation of starttime:
t_start = IIf(r_date <> r_date.Offset(-1, 0), r_time(1), r_end.Offset(-1, 0))
of course, as the name of the function is now different, it assigning result to function name lines have to reflect this:
takt_date = (t_end - t_start - t_break) * 24 * 60
Else
takt_date = "bad data"
F3 formula shall read:
=takt_date(E3,B3,$M$3:$N$3,$J$3:$K$5)
and copy down
in D3 you can either leave old version (if in VBA module you fave both functions) or use (if only nev version is in module).
=takt_date(C3,B3,$M$3:$N$3,$J$3:$K$5)
This does not address the issue you have shown in comments - I'm a bit "slow thinker", so write in "big letters" like:
time in F5 shall be 110 because it is the same day (B4 and B5), takes start time from .... and end time from E5(?) and includes all breaks in between.
whole code is:
Function takt_date(r_end As Range, r_date As Range, r_time As Range, r_breaks As Range) As Variant
Dim i As Integer, t_start As Double, t_end As Double, t_break As Double, breaks
Application.Volatile
If r_end.Cells.Count = 1 And r_time.Cells.Count = 2 And r_breaks.Columns.Count = 2 Then
t_start = IIf(r_date <> r_date.Offset(-1, 0), r_time(1), r_end.Offset(-1, 0)) 'here changed
t_end = WorksheetFunction.Min(r_end, r_time(2))
breaks = r_breaks.Value
For i = 1 To UBound(breaks) 'correction of start/end
breaks(i, 2) = breaks(i, 2) / (24 * 60) + breaks(i, 1)
If t_start >= breaks(i, 1) And t_start <= breaks(i, 2) Then t_start = breaks(i, 2)
If t_end >= breaks(i, 1) And t_end <= breaks(i, 2) Then t_end = breaks(i, 2)
Next i
For i = 1 To UBound(breaks) 'whole breaks inside
If t_start <= breaks(i, 1) And t_end >= breaks(i, 2) Then t_break = t_break + breaks(i, 2) - breaks(i, 1)
Next i
takt_date = (t_end - t_start - t_break) * 24 * 60
Else
takt_date = "bad data"
End If
End Function
Bookmarks