Private Sub Worksheet_Change(ByVal Target As Range)
Dim ecart#, nombre%, tablo, ub&, resu(), deb&, maxi#, i&, j%, n&
ecart = [H5]: nombre = [H6] 'à adapter
Application.ScreenUpdating = False
With [Tableau1] 'tableau structuré
.Sort .Columns(2), xlAscending, Header:=xlYes 'tri sur les quantités
tablo = .Resize(, 2) 'matrice, plus rapide
.Sort .Columns(1), xlAscending 'tri sur les villes
End With
ub = UBound(tablo)
ReDim resu(1 To ub, 1 To 1)
deb = 1
1 maxi = tablo(deb, 2) + ecart
For i = deb To ub
If tablo(i, 2) <= maxi And j < nombre Then
j = j + 1
If j = 1 Then n = n + 1: resu(n, 1) = "Amalgame " & n & " : "
resu(n, 1) = resu(n, 1) & IIf(j = 1, "", ", ") & tablo(i, 1) & " - " & tablo(i, 2)
Else
j = 0
deb = i
GoTo 1
End If
Next
'---restitution---
Application.EnableEvents = False 'désactive les évènements
With [D8] 'cellule à adapter
If n Then .Resize(n) = resu
.Offset(n).Resize(.Parent.Rows.Count - n - .Row + 1).ClearContents 'RAZ en dessous
End With
Application.EnableEvents = True 'réactive les évènements
End Sub