Private Sub Worksheet_Activate()
Worksheet_Change ActiveCell 'lance la macro
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim commande, tablo, d As Object, i&, resu(), source, lig&, matricule, s, ub%, a(), j%
commande = [C4]
'---liste sans doublon---
tablo = [A25].CurrentRegion.Resize(, 2) 'matrice, plus rapide, au moins 2 éléments
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare 'la casse est ignorée
For i = 3 To UBound(tablo) - 1
d(tablo(i, 1)) = i - 2 'mémorise la ligne
Next i
'---tableau des résultats---
ReDim resu(1 To UBound(tablo) - 3)
source = Sheets("Details").UsedRange.Resize(, 8) 'matrice, plus rapide
For i = 2 To UBound(source)
If d.exists(source(i, 6)) Then
If source(i, 8) = commande Then
lig = d(source(i, 6))
matricule = source(i, 2)
If InStr(resu(lig) & Chr(1), Chr(1) & matricule & Chr(1)) = 0 Then _
resu(lig) = resu(lig) & Chr(1) & matricule
End If
End If
Next i
'---restitution---
Application.ScreenUpdating = False
Application.EnableEvents = False 'désactive les évènements
With [L27].Resize(UBound(resu))
.Resize(, Columns.Count - .Column + 1).ClearContents 'RAZ
For i = 1 To UBound(resu)
s = Split(Mid(resu(i), 2), Chr(1))
ub = UBound(s)
If ub > -1 Then
ReDim a(ub)
For j = 0 To ub
If IsNumeric(s(j)) Then a(j) = CDbl(s(j)) Else a(j) = s(j)
Next j
tri a, 0, ub
.Cells(i).Resize(, ub + 1) = a
End If
Next i
End With
Application.EnableEvents = True 'réactive les évènements
End Sub
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