Function Delai(deb As Date, fin As Date) As Date
Dim dat As Long, t As Date, dur As Date
Application.Volatile
dat = Int(CDec(deb))
t = TimeValue(deb)
dur = PremDer(dat, t)
For dat = dat + 1 To Int(CDec(fin))
If IsError(Application.Match(dat, [Feries], 0)) Then dur = dur + [Ouverture].Cells(Weekday(dat, 2), 3)
Next
t = TimeValue(fin)
Delai = dur - PremDer(dat - 1, t)
End Function
Function PremDer(dat As Long, t As Date) As Date 'traite le 1er et le dernier jour
If IsNumeric(Application.Match(dat, [Feries], 0)) Or Weekday(dat, 2) > 5 Then Exit Function
Dim jour As Double, x As Double
jour = [Ouverture].Cells(Weekday(dat, 2), 3)
If t <= [Ouverture].Cells(Weekday(dat, 2), 1) Then PremDer = jour: Exit Function
If t <= [Pause].Cells(1) Then PremDer = jour - t + [Ouverture].Cells(Weekday(dat, 2), 1): Exit Function
x = [Pause].Cells(1) - [Ouverture].Cells(Weekday(dat, 2), 1)
If t <= [Pause].Cells(1, 2) Then PremDer = jour - x: Exit Function
If t <= [Pause].Cells(2, 1) Then PremDer = jour - x - t + [Pause].Cells(1, 2): Exit Function
x = x + [Pause].Cells(2, 1) - [Pause].Cells(1, 2)
If t <= [Pause].Cells(2, 2) Then PremDer = jour - x: Exit Function
If t <= [Pause].Cells(3, 1) Then PremDer = jour - x - t + [Pause].Cells(2, 2): Exit Function
x = x + [Pause].Cells(3, 1) - [Pause].Cells(2, 2)
If t <= [Pause].Cells(3, 2) Then PremDer = jour - x: Exit Function
If t <= [Ouverture].Cells(Weekday(dat, 2), 2) Then PremDer = jour - x - t + [Pause].Cells(3, 2)
End Function