Sub Tirages()
Dim Ntirages&, cible As Range, n&
Ntirages = 100000 'nombre de tirages, modifiable
Set cible = [O1]
Application.ScreenUpdating = False
With [B2:H13]
.Formula = "=RANDBETWEEN(0,3)" 'ALEA.ENTRE.BORNES
For n = 1 To Ntirages
If cible = True Then Exit For
Calculate 'nouveau tirage
Next
.Value = .Value 'supprime les formules
Feuil3.Range(.Address) = "=VLOOKUP(Feuil2!B2,Feuil2!$J$17:$K$20,2,0)"
Feuil3.Range(.Address) = Feuil3.Range(.Address).Value 'supprime les formules
End With
End Sub
Je ne comprends pas, il y a beaucoup plus de combinaisons possibles.Sur les 4^7 combinaisons possibles, il n’en reste que 252
Sub Tirages()
Dim Ntirages&, cible As Range, col%, n&
Ntirages = 10000 'nombre de tirages, modifiable
Set cible = [O1]
Application.ScreenUpdating = False
With [B2:H13]
.Value = "" 'RAZ
For col = 1 To 7
.Columns(col) = "=RANDBETWEEN(0,3)" 'ALEA.ENTRE.BORNES
For n = 1 To Ntirages
If cible = True Then .Columns(col) = .Columns(col).Value: Exit For
Calculate 'nouveau tirage
Next n, col
.Value = .Value 'supprime les formules s'il en reste
Feuil3.Range(.Address) = "=VLOOKUP(Feuil2!B2,Feuil2!$J$17:$K$20,2,0)"
Feuil3.Range(.Address) = Feuil3.Range(.Address).Value 'supprime les formules
End With
End Sub
Sub Tirages()
Dim col%, matin, soir, nuit, n&, c As Range
Application.ScreenUpdating = False
With [B2:H13]
.Value = 0 'RAZ
.Columns(1).EntireColumn.Insert
For col = 1 To 7
matin = .Cells(17, col): soir = .Cells(18, col): nuit = .Cells(19, col) 'à adapter
If col = 7 Then .Columns(1).Resize(, 6).Sort .Columns(6), xlDescending, Header:=xlNo
n = 0
For Each c In .Columns(col).Cells
If Not (CStr(c(1, 0)) = "0" And CStr(c(1, -1)) <> "0") Then
n = n + 1
c = IIf(n <= matin, 1, IIf(n <= matin + soir, 2, IIf(n <= matin + soir + nuit, 3, 0)))
End If
Next c
.Columns(1).Resize(, 9).Sort .Columns(9), Header:=xlNo 'tri aléatoire
Next col
.Columns(0).EntireColumn.Delete
Feuil3.Range(.Address) = "=VLOOKUP(Feuil2!B2,Feuil2!$J$17:$K$20,2,0)"
Feuil3.Range(.Address) = Feuil3.Range(.Address).Value 'supprime les formules
End With
End Sub
Sub Tirages()
Dim col%, matin, soir, nuit, n&, c As Range
Application.ScreenUpdating = False
With [B2:H13]
.Value = 0 'RAZ
.Columns(1).EntireColumn.Insert
.Columns(0) = "=RAND()" 'ALEA()
For col = 1 To 7
matin = .Cells(17, col): soir = .Cells(18, col): nuit = .Cells(19, col) 'à adapter
n = 0
For Each c In .Columns(col).Cells
If c(1, 0) <> 0 Or IIf(col < 7, c(1, -1) = 0, False) Then
n = n + 1
c = IIf(n <= matin, 1, IIf(n <= matin + soir, 2, IIf(n <= matin + soir + nuit, 3, 0)))
End If
Next c
.Columns(0).Resize(, 8).Sort .Columns(0), Header:=xlNo 'tri aléatoire
Next col
.Columns(0).EntireColumn.Delete
Feuil3.Range(.Address) = "=VLOOKUP(Feuil2!B2,Feuil2!$J$17:$K$20,2,0)"
Feuil3.Range(.Address) = Feuil3.Range(.Address).Value 'supprime les formules
End With
End Sub
Sub Tirages()
Dim Ntirages&, col%, matin, soir, nuit, n&, c As Range, b As Byte, tirage&
Application.ScreenUpdating = False
Ntirages = 1000 'modifiable
With [B2:H13]
.Columns(1).EntireColumn.Insert
.Columns(0) = "=RAND()" 'ALEA()
For col = 1 To 7
.Columns(col) = 0 'RAZ
matin = .Cells(17, col): soir = .Cells(18, col): nuit = .Cells(19, col) 'à adapter
n = 0
For Each c In .Columns(col).Cells
If c(1, 0) <> 0 Or IIf(col < 7, c(1, -1) = 0, False) Then
n = n + 1
b = IIf(n <= matin, 1, IIf(n <= matin + soir, 2, IIf(n <= matin + soir + nuit, 3, 0)))
If c(1, 0) & b = "31" Or c(1, 0) & b = "32" Then col = col - 1: tirage = tirage + 1: Exit For
c = b
End If
Next c
If tirage = Ntirages Then .Value = "": MsgBox " Recommencez...": Exit For
.Columns(0).Resize(, col + 1).Sort .Columns(0), Header:=xlNo 'tri aléatoire
Next col
.Columns(0).EntireColumn.Delete
Feuil3.Range(.Address) = "=VLOOKUP(Feuil2!B2,Feuil2!$J$17:$K$20,2,0)"
Feuil3.Range(.Address) = Feuil3.Range(.Address).Value 'supprime les formules
End With
End Sub
Oui j'ai eu du mal à trouver la bonne méthode.Cela fait trois jours que tu codes
Dim boucle As Boolean 'mémorise la variable
Sub Arret() 'bouton de sécurité
boucle = False
End Sub
Sub Tirages()
Dim cible As Range, etm#, Ntirages&, tirage&, col%, matin, soir, nuit, n&, c As Range, b As Byte, a
Application.ScreenUpdating = False
boucle = True
Set cible = [L1] 'à adapter
etm = [N1] 'à adapter, écart-type maximum
Ntirages = 100 'modifiable
With [B2:H13] 'à adapter
.Columns(1).EntireColumn.Insert
.Columns(0) = "=RAND()" 'ALEA()
Do
DoEvents 'permet le clic sur le bouton Arrêt
tirage = 0
For col = 1 To 7
.Columns(col) = 0 'RAZ
matin = .Cells(17, col): soir = .Cells(18, col): nuit = .Cells(19, col) 'lignes à adapter
n = 0
For Each c In .Columns(col).Cells
If c(1, 0) <> 0 Or IIf(col < 7, c(1, -1) = 0, False) Then
n = n + 1
b = IIf(n <= matin, 1, IIf(n <= matin + soir, 2, IIf(n <= matin + soir + nuit, 3, 0)))
If c(1, 0) & b = "31" Or c(1, 0) & b = "32" Then col = col - 1: tirage = tirage + 1: Exit For
c = b
End If
Next c
If tirage = Ntirages Then
If Not boucle Then .Value = "": .Columns(0).EntireColumn.Delete: MsgBox " Recommencez...": Exit Sub
Exit For
End If
.Columns(0).Resize(, col + 1).Sort .Columns(0), Header:=xlNo 'tri aléatoire
Next col
Loop While boucle And cible > etm Or tirage = Ntirages 'recherche des écarts-types < etm
.Columns(0).EntireColumn.Delete
'---formats personnalisés---
a = Array("""repos""", """matin""", """soir""", """nuit""")
For n = 0 To 3
.Replace n, ""
With .SpecialCells(xlCellTypeBlanks): .NumberFormat = "[=" & n & "]" & a(n): .Value = n: End With
Next n
End With
End Sub