je veux modifier mon code pour qu'il soit flexible , si par exemple je veux tirer aléatoirement sur un range et non pas toute la ligne
mon objectif est de sélectionner 2 min ou 3 max de personnes disponible le week end :)
voilà mon code :
Sub planning()
Const LIMIT = 1000000 ' limit iterations to solve
Dim wb As Workbook, ws As Worksheet
Dim lastrow As Long
Dim dict As Object, key, bLoop As Boolean
Dim n As Long, x As Long, sType As String
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "W", 7
dict.Add " ", 0
Set wb = ThisWorkbook
Set ws = wb.Sheets("planning")
bLoop = True
With ws
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("C2:C" & lastrow).Cells.Clear
Do While bLoop
' select random row
x = lastrow * Rnd() + 1
sType = Trim(.Cells(x, "B"))
' check if needed
If Len(.Cells(x, "C")) = 0 And dict(sType) > 0 Then
.Cells(x, "C") = "W"
dict(sType) = dict(sType) - 1
' check if finished
bLoop = False
For Each key In dict
If dict(key) > 0 Then bLoop = True
Next
End If
' avoid infinite loop
n = n + 1
If n > LIMIT Then
For Each key In dict.keys
Debug.Print key, dict(key)
Next
MsgBox "Too many iterations to solve", vbCritical, "limit=" & LIMIT
Exit Sub
End If
Loop
End With
MsgBox "Done in " & n & " iterations", vbInformation
End Sub