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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| Dim i As Long, j As Long, k As Long
Dim Lig As Long, C1 As Long, C2 As Long, C3 As Long
Dim DerLig As Long, DerLig_Pleine As Long
Dim a As Long, b As Long, c As Long, d As Long, e As Long, f As Long, n As Long, L1 As Long, L2 As Long
Dim A1 As Long, A2 As Long, A3 As Long, A4 As Long, A5 As Long, A6 As Long, A7 As Long
Dim Cpt As Double
Dim Deb As Double
Sub Combinaisons_et_TirageAleatoire()
Deb = Timer
Application.ScreenUpdating = False
C1 = 15
C2 = 16
C3 = 17
DerLig = Range("A" & Rows.Count).End(xlUp).Row
For n = 1 To 2
Range("B2:I" & DerLig).ClearContents
Lig = 2
'Combinaisons
For i = 2 To 7
For j = i + 1 To 7
If j = 9 Then j = 2
For k = 2 To 7
If Cells(i, C1) <> 1 And Cells(j, C2) <> 1 And Cells(k, C3) <> 6 And Cells(i, C1) <> Cells(j, C2) Then
Range(Cells(Lig, "E"), Cells(Lig, "G")) = Array(Cells(i, C1), Cells(j, C2), Cells(k, C3))
Lig = Lig + 1
End If
Next k
Next j
Next i
Next n
Construction:
'Construction par semaine
Construction_Par_Semaine
'Remplacement des N° par les affectations
For i = 1 To 7
Affect = Cells(i + 10, "T")
Range("B2:D" & DerLig).Replace What:=i, Replacement:=Affect, LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next i
'on reproduit le cycle pour les semaines manquantes, mais en permutant les colonnes B et C
DerLig_Pleine = Range("B" & Rows.Count).End(xlUp).Row
Range("B" & DerLig_Pleine + 1 & ":B" & DerLig).FormulaR1C1 = "=R[-" & DerLig_Pleine - 1 & "]C[1]"
Range("C" & DerLig_Pleine + 1 & ":C" & DerLig).FormulaR1C1 = "=R[-" & DerLig_Pleine - 1 & "]C[-1]"
Range("D" & DerLig_Pleine + 1 & ":D" & DerLig).FormulaR1C1 = "=R[-" & DerLig_Pleine - 1 & "]C"
Range("B2:D" & DerLig).Value = Range("B2:D" & DerLig).Value
Range("E2:J" & DerLig).ClearContents
MsgBox "Durée: " & Timer - Deb & " Sec" & Chr(10) & Cpt & "Combinaisons parcourues"
End Sub
Sub Construction_Par_Semaine()
Cpt = 0
Range("O11:Q18").ClearContents
i = 2
Randomize
For a = 2 To 31
Range(Cells(11, "O"), Cells(11, "Q")).Value = Range(Cells(a, "E"), Cells(a, "G")).Value
For b = 32 To 55
Range(Cells(12, "O"), Cells(12, "Q")).Value = Range(Cells(b, "E"), Cells(b, "G")).Value
For c = 56 To 73
Range(Cells(13, "O"), Cells(13, "Q")).Value = Range(Cells(c, "E"), Cells(c, "G")).Value
For d = 74 To 85
Range(Cells(14, "O"), Cells(14, "Q")).Value = Range(Cells(d, "E"), Cells(d, "G")).Value
For e = 86 To 91
Range(Cells(15, "O"), Cells(15, "Q")).Value = Range(Cells(e, "E"), Cells(e, "G")).Value
Cpt = Cpt + 1
Cherche:
f = Int((90 * Rnd) + 1) + 1
Range(Cells(16, "O"), Cells(16, "Q")).Value = Range(Cells(f, "E"), Cells(f, "G")).Value
'si il y a 2 journées identiques, on refait une recherche aléatoire
If Cells(16, "N") = Cells(11, "N") Or Cells(16, "N") = Cells(12, "N") Or Cells(16, "N") = Cells(13, "N") Or _
Cells(16, "N") = Cells(14, "N") Or Cells(16, "N") = Cells(15, "N") Then GoTo Cherche
A1 = Application.CountIf(Range("O11:Q16"), 1)
A2 = Application.CountIf(Range("O11:Q16"), 2)
A3 = Application.CountIf(Range("O11:Q16"), 3)
A4 = Application.CountIf(Range("O11:Q16"), 4)
A5 = Application.CountIf(Range("O11:Q16"), 5)
A6 = Application.CountIf(Range("O11:Q16"), 6)
A7 = Application.CountIf(Range("O11:Q16"), 7)
'on ne conserve le résultat que si chaque employé ne soit pas présent plus de 3 fois dans la semaine et que tous soient présent au moins 1 fois
If Application.Max(A1, A2, A3, A4, A5, A6, A7) <= 3 And Application.Min(A1, A2, A3, A4, A5, A6, A7) <> 0 Then
L1 = Int((5 * Rnd) + 1) + 10
L2 = Int((5 * Rnd) + 1) + 10
Do While L1 = L2
L2 = Int((5 * Rnd) + 1) + 10
Loop
'on permute les lignes L1 et L2 pour obtenir un effet aléatoire
Range(Cells(18, "O"), Cells(18, "Q")).Value = Range(Cells(L1, "O"), Cells(L1, "Q")).Value
Range(Cells(L1, "O"), Cells(L1, "Q")).Value = Range(Cells(L2, "O"), Cells(L2, "Q")).Value
Range(Cells(L2, "O"), Cells(L2, "Q")).Value = Range(Cells(18, "O"), Cells(18, "Q")).Value
Range(Cells(i, "B"), Cells(i + 5, "D")).Value = Range("O11:Q16").Value
Range("O11:Q18").ClearContents
i = i + 6
GoTo Suivant_a
End If
Suivant_e:
Next e
Suivant_d:
Next d
Suivant_c:
Next c
Suivant_b:
Next b
Suivant_a:
Next a
End Sub |
Partager