Function Calc_FinishDate_INTL5(sta As Double, dur As Double, Kal As String, holidees As Range, SF_flag As String) As Double
Dim sec_1 As Double, sec_1000ste As Double, dt1 As Double, wd As Integer, sfflg As Integer, OfSet As Long
Dim du1 As Double, Old_dt1 As Double, i As Long, STime(8) As Double, FTime(8) As Double, ofst As Integer
If wt_flg <> True Then
wtpd = Range("Werktijden_pd").Value
wt_flg = True
End If
ofst = (Asc(Kal) - 65 + 1) * 9 - 6 '=(CODE($L25)-65+1)*9-6
For i = 1 To 7
STime(i) = wtpd(i + ofst, 1)
FTime(i) = wtpd(i + ofst, 2)
Next i
sec_1 = 1 / 24 / 60 / 60
sec_1000ste = sec_1 / 1000
If SF_flag <> "S" And SF_flag <> "F" Then SF_flag = "F"
sfflg = -1: If SF_flag <> "F" Then sfflg = 1
If dur = 0 Then 'was 0
If SF_flag = "S" Then dt1 = Calc_FinishDate_INTL5(sta, sec_1, Kal, holidees, SF_flag) - sec_1: GoTo Uit1
If SF_flag = "F" Then dt1 = Calc_StartDate_INTL5(sta, sec_1, Kal, holidees, SF_flag) + sec_1: GoTo Uit1
End If
If dur < 0 Then dt1 = "FOUT": GoTo Uit1
sta = Int(sta / sec_1 + 0.5) * sec_1 'afronden op hele seconde
dur = Int(dur / sec_1 + 0.5) * sec_1 'afronden op hele seconde
OfSet = 35000
dt1 = sta
wd = Weekday(dt1, vbMonday)
du1 = 0
'------------------------------------------------------stap-1
If dur = 0 Then GoTo Uit1
'------------------------------------------------------stap0
opnieuw1:
If dt1 <= Int(dt1) + STime(wd) Then
dt1 = dt1 - 1
Do
dt1 = dt1 + 1: wd = Weekday(dt1, vbMonday)
Loop Until FTime(wd) > (STime(wd) + sec_1) And holidees(Int(dt1) - OfSet, 1) <> 1
dt1 = Int(dt1) + STime(wd)
End If
'------------------------------------------------------stap 1
wd = Weekday(dt1, vbMonday)
If dt1 > Int(dt1) + FTime(wd) Or Abs(FTime(wd) - STime(wd)) < sec_1 Or holidees(Int(dt1) - OfSet, 1) = 1 Then
Do
dt1 = dt1 + 1: wd = Weekday(dt1, vbMonday)
Loop Until FTime(wd) > (STime(wd) + sec_1) And holidees(Int(dt1) - OfSet, 1) <> 1
dt1 = Int(dt1) + STime(wd): wd = Weekday(dt1, vbMonday)
End If
'------------------------------------------------------stap2
wd = Weekday(dt1, vbMonday)
If dt1 >= Int(dt1) + STime(wd) Then
dt1 = dt1 - 1
Do
dt1 = dt1 + 1: wd = Weekday(dt1, vbMonday)
Loop Until FTime(wd) > (STime(wd) + sec_1) And holidees(Int(dt1) - OfSet, 1) <> 1
Old_dt1 = dt1
wd = Weekday(dt1, vbMonday)
dt1 = Int(dt1) + FTime(wd)
du1 = du1 + (dt1 - Old_dt1): du1 = Int(du1 / sec_1 + 0.5) * sec_1
If du1 >= (dur + sfflg * sec_1000ste) Then
dt1 = dt1 + dur - du1
GoTo Uit1
End If
End If
'------------------------------------------------------stap3
Stap3:
Do
dt1 = dt1 + 1: wd = Weekday(dt1, vbMonday)
Loop Until FTime(wd) > (STime(wd) + sec_1) And holidees(Int(dt1) - OfSet, 1) <> 1
wd = Weekday(dt1, vbMonday)
du1 = du1 + FTime(wd) - STime(wd): du1 = Int(du1 / sec_1 + 0.5) * sec_1
If du1 >= (dur + sfflg * sec_1000ste) Then
If dt1 > Int(dt1) + FTime(wd) Then dt1 = Int(dt1) + FTime(wd) 'toegevoegd op 12-9-2018
dt1 = dt1 + dur - du1
GoTo Uit1
End If
GoTo Stap3
Uit1:
Calc_FinishDate_INTL5 = Int(dt1 / sec_1 + 0.5) * sec_1
End Function