Option Explicit
Dim a()
Sub Essai()
Dim t, i&, deb, xcible&, xpas&
deb = Timer
'effacement des précédents résultats
Range("h1").CurrentRegion.Clear
'lecture des données
t = Range(Cells(1, "a"), Cells(Rows.Count, "a").End(xlUp))
ReDim a(0 To 0)
For i = 1 To UBound(t)
If t(i, 1) > 0 And t(i, 1) <= Range("Cible") Then
ReDim Preserve a(0 To UBound(a) + 1)
a(UBound(a)) = t(i, 1)
End If
Next i
Randomize
For xcible = Range("Cible") To 1 Step -1
For xpas = Range("MaxPaquet") To 1 Step -1
nTours xcible, xpas, Range("Itérations")
Next xpas
Next xcible
'calcul des totaux
i = Cells(Rows.Count, "h").End(xlUp).Row
Range("g2:g" & i).FormulaR1C1 = "=SUM(RC[1]:RC[20])"
Range("g2:g" & i).Font.Bold = True
Range("g2:g" & i).Interior.Color = RGB(200, 200, 200)
Range("g2").CurrentRegion.Borders.LineStyle = xlContinuous
MsgBox "Durée = " & Format(Timer - deb, "#,##0.0\ sec.")
End Sub
Sub nTours(Cible, pas, nFois)
Dim i&
For i = 1 To nFois
UnTour Cible, pas
Next i
End Sub
Sub UnTour(Cible, pas)
Dim i&, som&, j&, ligne&, n&, aux&, col&
For i = 1 To UBound(a)
n = 1 + Int(Rnd * UBound(a))
aux = a(i): a(i) = a(n): a(n) = aux
Next i
For i = 1 To Int(UBound(a) / pas)
som = 0
For j = 1 + pas * (i - 1) To 1 + pas * (i - 1) + (pas - 1): som = som + a(j): Next
If som = Cible Then
ligne = Cells(Rows.Count, "h").End(xlUp).Row + 1: col = Range("h1").Column
For j = 1 + pas * (i - 1) To 1 + pas * (i - 1) + (pas - 1)
Cells(ligne, col) = a(j)
col = col + 1
a(j) = ""
Next j
End If
Next i
compacter
End Sub
Sub compacter()
Dim i&, n&
For i = 1 To UBound(a)
If a(i) <> "" Then n = n + 1: a(n) = a(i)
Next i
ReDim Preserve a(0 To n)
End Sub