Sub TiragesAléatoires()
Dim durée#, Ntirage&, a1, ub1 As Byte, a2, ub2 As Byte, Nmax&, derlig&
Dim P As Range, tablo, ub&, t, i As Byte, x, y, j As Byte, z, k&, n&
durée = Timer
Ntirage = 1000
a1 = Sheets("liste").[A5:B10]
ub1 = UBound(a1) '6
a2 = Array("a", "b", "c")
ub2 = UBound(a2) '2
Ntirage = Application.Ceiling(Ntirage, ub1 * (ub2 + 1)) '1008, multiple de 6 x 3
Nmax = Ntirage / ub1 / (ub2 + 1) '56
derlig = Sheets("base").Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With Feuil3 'CodeName de la feuille de restitution
.Rows("4:" & .Rows.Count).Delete 'RAZ
Sheets("base").Rows("4:" & derlig).Copy .[A4]
Set P = Intersect(.UsedRange, .Rows("4:" & derlig))
P.Columns(15).Offset(1).ClearContents 'RAZ colonne O, au cas où...
P.Columns(16).Offset(1).ClearContents 'RAZ colonne P
With P.Columns(P.Columns.Count + 1) 'colonne auxiliaire à droite
.Formula = "=RAND()"
.Value = .Value 'supprime les formules
.Cells(1) = 0
Union(P, .Cells).Sort .Cells, xlAscending '1er tri
.Cells = ""
.Cells(1) = 0
tablo = P 'matrice du tableau
ub = UBound(tablo)
t = .Cells 'matrice de la colonne auxiliaire
For i = 1 To ub1
x = a1(i, 1): y = a1(i, 2)
For j = 0 To ub2
z = a2(j): n = 0
For k = 2 To ub
If tablo(k, 3) = z And t(k, 1) = "" Then
tablo(k, 15) = x
tablo(k, 16) = y
t(k, 1) = 0
n = n + 1
If n = Nmax Then Exit For
End If
Next
Next
Next
.Cells = t
P.Columns(15) = Application.Index(tablo, , 15)
P.Columns((16)) = Application.Index(tablo, , 16)
Union(P, .Cells).Sort .Cells '2ème tri
.EntireColumn.Delete 'suppression de la colonne auxiliaire
End With
.Rows(Ntirage + 5 & ":" & .Rows.Count).Delete
n = .UsedRange.Rows.Count 'ajuste la barre de défilement verticale
.Columns.AutoFit 'ajustement de la largeur des colonnes
.Activate
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
MsgBox Ntirage & " tirages" & vbLf & vbLf & _
"Durée " & Format(Timer - durée, "0.00 \s"), , "Tirages"
End Sub