1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| Sub AssignerPostes()
Dim wsCandidats As Worksheet
Dim wsPostes As Worksheet
Dim rngCandidats As Range
Dim rngPostes As Range
Dim cell As Range
Dim postes As Collection
Dim poste As Range
Dim randomIndex As Long
' Feuilles de travail
Set wsCandidats = ThisWorkbook.Sheets("candidats")
Set wsPostes = ThisWorkbook.Sheets("poste à attribuer")
' Plages de données
Set rngCandidats = wsCandidats.Range("C2:C" & wsCandidats.Cells(wsCandidats.Rows.Count, "C").End(xlUp).Row)
Set rngPostes = wsPostes.Range("A2:A" & wsPostes.Cells(wsPostes.Rows.Count, "A").End(xlUp).Row)
' Parcourir chaque candidat
For Each cell In rngCandidats
Set postes = New Collection
' Trouver les postes correspondants
For Each poste In rngPostes
If poste.Value = cell.Value Then
postes.Add poste.Offset(0, 1).Value
End If
Next poste
' Attribuer un poste aléatoire
If postes.Count > 0 Then
randomIndex = Int((postes.Count * Rnd) + 1)
cell.Offset(0, 1).Value = postes(randomIndex)
End If
Next cell
End Sub |