Sub Groupes()
Dim t, nbg#, i&, n&, k&, aux
On Error GoTo FIN:
Application.ScreenUpdating = False
With Sheets("Feuil1")
.Select
If [g1] = "" Then Exit Sub Else nbg = [g1]
If [e1] = "TempAuxE" Then [e1].EntireColumn.Delete
Columns("e:e").Insert: [e1] = "TempAuxE"
If .FilterMode Then .ShowAllData
t = [a1].CurrentRegion
Randomize: For i = 2 To UBound(t): t(i, 5) = Rnd: Next
[a1].Resize(UBound(t), UBound(t, 2)) = t
[a1].CurrentRegion.Sort key1:=[e1], order1:=xlAscending, MatchCase:=False, Header:=xlYes
[a1].CurrentRegion.Sort key1:=[c1], order1:=xlAscending, MatchCase:=False, Header:=xlYes
[e2].Resize(UBound(t) - 1).FormulaLocal = "=SI(C2<>C1;ALEA();E1)"
[a1].CurrentRegion.Sort key1:=[e1], order1:=xlAscending, MatchCase:=False, Header:=xlYes
t = [a1].CurrentRegion: n = 1 + Int(Rnd * nbg)
For i = 2 To UBound(t)
n = n + 1: If n = nbg + 1 Then n = 1
t(i, 4) = n
Next i
ReDim r(1 To nbg)
For i = 1 To nbg: r(i) = i: Next
For i = 1 To nbg: k = 1 + Int(Rnd * nbg): aux = r(i): r(i) = r(k): r(k) = aux: Next i
For i = 2 To UBound(t): t(i, 4) = r(t(i, 4)): Next
[a1].Resize(UBound(t), UBound(t, 2)) = t
[a1].CurrentRegion.Sort key1:=[c1], order1:=xlAscending, MatchCase:=False, Header:=xlYes, _
key2:=[d1], order2:=xlAscending
FIN:
If [e1] = "TempAuxE" Then [e1].EntireColumn.Delete
End With
End Sub