Function Nb_Jours_Moyen(nomref As String, Noms As Range, Dates As Range)
Dim i&, a(), n&, b()
For i = 1 To Noms.Count
If Noms(i) = nomref Then
ReDim Preserve a(n) 'base 0
a(n) = Dates(i)
n = n + 1
End If
Next i
If n = 0 Then Nb_Jours_Moyen = "": Exit Function
If n = 1 Then Nb_Jours_Moyen = 1 + Date - a(0): Exit Function
tri a, 0, n - 1
ReDim b(1 To n - 1) 'base 1
For i = 1 To n - 1
b(i) = a(i) - a(i - 1)
Next i
Nb_Jours_Moyen = 1 + Application.Average(b)
End Function
Sub tri(a, gauc, droi) ' Quick sort
Dim ref, g, d, temp
ref = a((gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While a(g) < ref: g = g + 1: Loop
Do While ref < a(d): d = d - 1: Loop
If g <= d Then
temp = a(g): a(g) = a(d): a(d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call tri(a, g, droi)
If gauc < d Then Call tri(a, gauc, d)
End Sub