Private Sub Worksheet_Change(ByVal Target As Range)
Dim tablo, ub&, i&, x, prix1, prix0, j&
Application.ScreenUpdating = False
Application.EnableEvents = False 'désactive les évènements
With ListObjects(1).Range 'tableau structuré
.AutoFilter: .AutoFilter 'si le tableau est filtré
.Columns(8).Insert xlToRight 'insère une colonne auxiliaire
.Cells(2, 8) = "=N(R[-1]C)+1": .Columns(8) = .Columns(8).Value 'numérotation des lignes
.Sort .Columns(1), xlAscending, .Columns(2), , xlDescending, Header:=xlYes 'tri sur 2 colonnes
tablo = .Resize(, 8) 'matrice, plus rapide
ub = UBound(tablo)
For i = 2 To ub
x = tablo(i, 1)
If x <> tablo(i - 1, 1) Then
prix1 = tablo(i, 4)
tablo(i, 7) = prix1
If i = ub Then
tablo(i, 6) = "n/a"
Else
If tablo(i + 1, 1) <> x Then
tablo(i, 6) = "n/a"
Else
prix0 = tablo(i + 1, 4)
tablo(i, 6) = prix0
For j = i + 1 To ub
If tablo(j, 1) <> x Then Exit For
tablo(j, 6) = prix0
tablo(j, 7) = prix1
Next j
i = j - 1
End If
End If
End If
Next i
.Columns(6) = Application.Index(tablo, , 6) 'restitution
.Columns(7) = Application.Index(tablo, , 7) 'restitution
.Sort .Columns(8), xlAscending, Header:=xlYes 'tri dans l'ordre initial
.Columns(8).Delete xlToLeft 'supprime la colonne auxiliaire
End With
Application.EnableEvents = True 'réactive les évènements
End Sub