Option Compare Text 'la casse est ignorée
Private Sub Worksheet_Activate()
Dim c As Range, x$, crit$, n&, i&, a(), b
Randomize
Application.ScreenUpdating = False
With [Tableau1].ListObject.Range 'tableau structuré
.Sort .Columns(1), xlAscending, Header:=xlYes 'tri du tableau
For Each c In UsedRange
If c = "N° Equipe" Then
x = Trim(Replace(c(0, 0), "POULE", ""))
crit = x & "*"
n = Application.CountIf(.Columns(1), crit)
c(2).Resize(4, 2) = "" 'RAZ
If x <> "" And n Then
ReDim a(1 To n)
For i = 1 To n: a(i) = Rnd: Next i 'nombres aléatoires
b = .Cells(Application.Match(crit, .Columns(1), 0), 1).Resize(n, 2) 'au moins 2 éléments
tri a, b, 1, n
With c(2).Resize(IIf(n < 4, n, 4))
.Value = b 'restitution
.Replace x, "", xlPart
End With
End If
End If
Next
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, 1): b(g, 1) = b(d, 1): b(d, 1) = 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