Private Sub Worksheet_Calculate()
Dim t, i&, n&, a(), b(), d As Object, m%
With [A1].CurrentRegion 'adapter éventuellement
t = .Resize(, 4) 'matrice, plus rapide
For i = 2 To UBound(t)
If Rows(i).Hidden Then
t(i, 4) = "" 'RAZ
Else
ReDim Preserve a(n): ReDim Preserve b(n) 'base 0
a(n) = t(i, 3) + i / "1E9" 'différenciation des jours pour le tri
b(n) = i 'repérage de la ligne
n = n + 1
End If
Next
If n = 0 Then Exit Sub
tri a, b, 0, UBound(a)
Set d = CreateObject("Scripting.Dictionary")
For i = 0 To UBound(a)
If b(i) Then
m = Year(Round(a(i))) 'millésime
If d.exists(m) Then
d(m) = "J" & Mid(d(m), 2) + Sgn(Round(a(i) - a(i - 1)))
Else
d(m) = "J1"
End If
t(b(i), 4) = d(m)
End If
Next
'---restitution---
Application.ScreenUpdating = False
Application.EnableEvents = False
For i = 2 To UBound(t)
.Cells(i, 4) = t(i, 4)
Next
Application.EnableEvents = True
Application.ScreenUpdating = True
End With
End Sub
Sub tri(a, b, 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
temp = b(g): b(g) = b(d): b(d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call tri(a, b, g, droi)
If gauc < d Then Call tri(a, b, gauc, d)
End Sub