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, t5 As Date, t6 As Date
Dim jour1 As Date, jour2 As Date, jour3 As Date, dat&, t As Date, dur As Date, wd As Byte, d#
t1 = horaire(1)
t2 = horaire(1, 2)
t3 = horaire(2, 1)
t4 = horaire(2, 2)
t5 = horaire(3, 1)
t6 = horaire(3, 2)
jour1 = t2 - t1: jour2 = t4 - t3: jour3 = t6 - t5
If jour1 < TimeValue("0:01") Or jour2 < TimeValue("0:01") Or jour3 < 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) < 4 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) = 4 Then
If t <= t3 Then dur = jour2
If t > t3 And t < t4 Then dur = t4 - t
ElseIf IsError(Application.Match(dat, feries, 0)) And Weekday(dat, 2) = 5 Then
If t <= t5 Then dur = jour3
If t > t5 And t < t6 Then dur = t6 - t
End If
While dur < duree
dat = dat + 1
wd = Weekday(dat, 2)
If IsError(Application.Match(dat, feries, 0)) And wd < 6 _
Then dur = dur + IIf(wd < 4, jour1, IIf(wd = 4, jour2, jour3))
Wend
d = dur - duree
Datefin = dat + IIf(wd < 4, t2, IIf(wd = 4, t4, t6)) - d
End Function