Function Datefin(deb As Date, duree As Date, horaire As Range, feries As Range) As Date
Dim t1 As Date, t2 As Date, t3 As Date, t4 As Date, jour1 As Date, jour2 As Date
Dim dat&, t As Date, dur As Date, d#
t1 = horaire(1)
t2 = horaire(1, 2)
t3 = horaire(2, 1)
t4 = horaire(2, 2)
jour1 = t2 - t1: jour2 = t4 - t3
If jour1 < TimeValue("0:01") Or jour2 < TimeValue("0:01") Then End 'sécurité
If duree <= 0 Then duree = 10 ^ -9
dat = Int(CDbl(deb))
t = TimeValue(deb)
If IsError(Application.Match(dat, feries, 0)) And Weekday(dat, 2) < 5 Then
If t <= t1 Then dur = jour1
If t > t1 And t < t2 Then dur = t2 - t
ElseIf IsError(Application.Match(dat, feries, 0)) And Weekday(dat, 2) = 5 Then
If t <= t3 Then dur = jour2
If t > t3 And t < t4 Then dur = t4 - t
End If
While dur < duree
dat = dat + 1
If IsError(Application.Match(dat, feries, 0)) And Weekday(dat, 2) < 6 _
Then dur = dur + IIf(Weekday(dat, 2) < 5, jour1, jour2)
Wend
d = dur - duree
Datefin = dat + IIf(Weekday(dat, 2) < 5, t2, t4) - d
End Function