Function Avancement(datecalcul As Range, deb As Date, fin 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, dat&, t As Date
fin = IIf(datecalcul < fin, datecalcul, fin)
If fin < deb Then Exit Function
t1 = horaire(1): t2 = horaire(1, 2): jour1 = t2 - t1
t3 = horaire(2, 1): t4 = horaire(2, 2): jour2 = t4 - t3
If Int(deb) = Int(fin) Then
If t1 < TimeValue(deb) Then t1 = TimeValue(deb)
If t2 < t1 Then t2 = t1
If t3 < TimeValue(deb) Then t3 = TimeValue(deb)
If t4 < t3 Then t4 = t3
End If
For dat = Int(deb) To Int(fin)
If Application.CountIf(feries, dat) = 0 And Weekday(dat, 2) < 6 Then
If dat = Int(deb) And dat <> Int(fin) Then
t = TimeValue(deb)
If Weekday(dat, 2) < 5 Then
If t <= t1 Then Avancement = Avancement + jour1
If t > t1 And t < t2 Then Avancement = Avancement + t2 - t
ElseIf Weekday(dat, 2) = 5 Then
If t <= t3 Then Avancement = Avancement + jour2
If t > t3 And t < t4 Then Avancement = Avancement + t4 - t
End If
ElseIf dat = Int(fin) Then
t = TimeValue(fin)
If Weekday(dat, 2) < 5 Then
If t >= t2 Then Avancement = Avancement + t2 - t1
If t > t1 And t < t2 Then Avancement = Avancement + t - t1
ElseIf Weekday(dat, 2) = 5 Then
If t >= t4 Then Avancement = Avancement + t4 - t3
If t > t3 And t < t4 Then Avancement = Avancement + t - t3
End If
Else
Avancement = Avancement + IIf(Weekday(dat, 2) < 5, jour1, jour2)
End If
End If
Next
End Function