Sub RepartirPersonnes()
Dim WsBDD As Worksheet
Dim i As Long, j As Long
Dim Nom As String, Grade As String, villeAttribuee As String
Dim choix(1 To 3) As String
Dim Villes() As Variant 'tablo VBA
Dim MaxCompagnon As Long, MaxAspirant As Long, MaxJeune As Long
Dim VilleTestée As String
Set WsBDD = ThisWorkbook.Sheets("BDD") ' Remplacez "Feuille1" par le nom de votre feuille
Set TSBDD = WsBDD.ListObjects("t_BDD")
With TSBDD
.ListColumns("Ville Attribuée").DataBodyRange.ClearContents 'on efface la colonne "Ville Attribuée"
TabBDD = .DataBodyRange.Value 'on met le contenu de la TS dans le Tablo vba
End With
Set DicoCompteurs = CreateObject("Scripting.dictionary") 'création du dictionnaire des compteurs
Application.ScreenUpdating = False 'désactive le refresh écran pour éviter l'effet sapin de noel
' Initialiser les villes
With Sheets("Listes")
Villes = .ListObjects("t_Villes").DataBodyRange.Value 'on remplit le tableau Ville avec les données de la TS
End With
For i = LBound(Villes, 1) To UBound(Villes, 1) 'pour chaque ville
With WsBDD.ListObjects("t_Capacité") 'on récupère les capacités de la ville pour chaque grade
MaxCompagnon = .ListColumns(Villes(i, 1)).DataBodyRange(1)
MaxAspirant = .ListColumns(Villes(i, 1)).DataBodyRange(2)
MaxJeune = .ListColumns(Villes(i, 1)).DataBodyRange(3)
End With
DicoCompteurs.Add Villes(i, 1), Array(MaxCompagnon, MaxAspirant, MaxJeune) 'on ajoute la ville dans le dictionnaire en y associant un array des compteurs initiaux
Next i
' Parcourir chaque personne dans le tableau==> on travaille directement sur le tablo VBA = plus rapide
For i = LBound(TabBDD, 1) To UBound(TabBDD, 1) 'pour chaque ligne du tableau
Nom = TabBDD(i, 1)
Grade = TabBDD(i, 2)
For j = 1 To 3 'on récupère les 3 choix
choix(j) = TabBDD(i, j + 2)
Next j
' Essayer d'attribuer une ville selon les choix
villeAttribuee = ""
For j = 1 To 3 'pour chaque choix
If Not VilleDejaFait(choix(j), i) Then ' Vérifier si la ville a déjà été faite
If PeutAttribuer(CStr(choix(j)), Grade) Then ' Vérifier si le grade peut être attribué
villeAttribuee = choix(j)
TabBDD(i, 16) = villeAttribuee 'on place la ville attribuée
Exit For 'pas besoin de continuer à parcourir les choix
End If
End If
Next j
If villeAttribuee = "" Then 'on regarde le choix de travail
ChoixTravail = TabBDD(i, 7)
If ChoixTravail <> "" Then
'pour chaque ville offrant la formation
With Sheets("Formation-travail").ListObjects("t_FormationTravail")
For j = 1 To .ListRows.Count
If UCase(.ListColumns(ChoixTravail).DataBodyRange(j)) = "X" Then
VilleTestée = .DataBodyRange(j, 1)
If Not VilleDejaFait(VilleTestée, i) Then
If PeutAttribuer(VilleTestée, Grade) Then
villeAttribuee = VilleTestée
TabBDD(i, 16) = villeAttribuee 'on place la ville attribuée
Exit For 'pas besoin de continuer à parcourir les choix
End If
End If
End If
Next j
End With
End If
End If
If villeAttribuee = "" Then 'on regarde le choix de formation
ChoixFormation = TabBDD(i, 6)
If ChoixFormation <> "" Then
'pour chaque ville offrant la formation
With Sheets("Formation-travail").ListObjects("t_FormationTravail")
For j = 1 To .ListRows.Count
If UCase(.ListColumns(ChoixFormation).DataBodyRange(j)) = "X" Then
VilleTestée = .DataBodyRange(j, 1)
If Not VilleDejaFait(VilleTestée, i) Then
If PeutAttribuer(VilleTestée, Grade) Then
villeAttribuee = VilleTestée
TabBDD(i, 16) = villeAttribuee 'on place la ville attribuée
Exit For 'pas besoin de continuer à parcourir les choix
End If
End If
End If
Next j
End With
End If
End If
Next i
With TSBDD
.DataBodyRange = TabBDD 'on colle le résultat dans la TS
End With
Application.ScreenUpdating = True
MsgBox "Opération Terminée"
End Sub