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 110 111 112 113 114 115 116 117 118 119 120
| Sub CreerTableau()
Dim mois As Integer
Dim annee As Integer
Dim dernierJour As Integer
Dim dateCourante As Date
Dim coef As Double
Dim j As Integer
Dim h As Integer
'Demander le mois et l'année à l'utilisateur
mois = InputBox("Entrez le mois (1-12):")
annee = InputBox("Entrez l'année:")
'Vérifier si la feuille existe déjà
Dim feuilleExiste As Boolean
feuilleExiste = False
Dim feuille As Worksheet
For Each feuille In ThisWorkbook.Worksheets
If feuille.Name = "Tableau " & mois & "-" & annee Then
feuilleExiste = True
Exit For
End If
Next feuille
If feuilleExiste Then
MsgBox "La feuille existe déjà. Veuillez choisir un autre mois et/ou année."
Exit Sub
End If
'Déterminer le dernier jour du mois courant
dernierJour = Day(DateSerial(annee, mois + 1, 0))
'Créer une nouvelle feuille pour le tableau
Set feuille = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
feuille.Name = "Tableau " & mois & "-" & annee
'Remplir les en-têtes de colonne
feuille.Cells(1, 1).Value = "Jour"
feuille.Cells(1, 2).Value = "Heure"
feuille.Cells(1, 3).Value = "Coefficient de Pondération"
'Formater la colonne C en nombre à deux décimales
feuille.Columns("C:C").NumberFormat = "0.00"
'Remplir le tableau avec les données
For j = 1 To dernierJour
For h = 7 To 22
dateCourante = DateSerial(annee, mois, j) + TimeSerial(h, 0, 0)
If Weekday(dateCourante, vbMonday) <= 5 And Not (Holiday(dateCourante)) Then
'Jour ouvrable
If h < 17 Then
coef = 1
Else
coef = 1.5
End If
Else
'Férié ou week-end
If h < 17 Then
coef = 1
Else
coef = 1.34
End If
End If
feuille.Cells((j - 1) * 24 + h - 6 + 1, 1).Value = j
feuille.Cells((j - 1) * 24 + h - 6 + 1, 2).Value = Format(TimeSerial(h, 0, 0), "hh""h"" - ") & Format(TimeSerial(h + 1, 0, 0), "hh""h""") ' plage horaire
feuille.Cells((j - 1) * 24 + h - 6 + 1, 3).Value = coef
Next h
'Ajouter la plage horaire de 23h à 24h
dateCourante = DateSerial(annee, mois, j) + TimeSerial(23, 0, 0)
If Weekday(dateCourante, vbMonday) <= 5 And Not (Holiday(dateCourante)) Then
'Jour ouvrable
coef = 1.5
Else
'Férié ou week-end
coef = 1.34
End If
feuille.Cells((j - 1) * 24 + 23 - 6 + 1, 1).Value = j
feuille.Cells((j - 1) * 24 + 23 - 6 + 1, 2).Value = "23h - 24h"
feuille.Cells((j - 1) * 24 + 23 - 6 + 1, 3).Value = coef
'Ajouter la plage horaire de 0h à 1h
dateCourante = DateSerial(annee, mois, j) + TimeSerial(0, 0, 0)
If Weekday(dateCourante, vbMonday) <= 5 And Not (Holiday(dateCourante)) Then
'Jour ouvrable
coef = 0.5
Else
'Férié ou week-end
coef = 0.66
End If
feuille.Cells((j - 1) * 24 + 24 - 6 + 1, 1).Value = j
feuille.Cells((j - 1) * 24 + 24 - 6 + 1, 2).Value = "00h - 01h"
feuille.Cells((j - 1) * 24 + 24 - 6 + 1, 3).Value = coef
For h = 1 To 6
dateCourante = DateSerial(annee, mois, j) + TimeSerial(h, 0, 0)
If Weekday(dateCourante, vbMonday) <= 5 And Not (Holiday(dateCourante)) Then
'Jour ouvrable
coef = 0.5
Else
'Férié ou week-end
coef = 0.66
End If
feuille.Cells((j - 1) * 24 + h + 24 - 6 + 1, 1).Value = j
feuille.Cells((j - 1) * 24 + h + 24 - 6 + 1, 2).Value = Format(TimeSerial(h, 0, 0), "hh""h"" - ") & Format(TimeSerial(h + 1, 0, 0), "hh""h""") ' plage horaire
feuille.Cells((j - 1) * 24 + h + 24 - 6 + 1, 3).Value = coef
Next h
Next j
'Mettre en forme le tableau
feuille.Columns.AutoFit
feuille.Rows(1).Font.Bold = True
End Sub
Function Holiday(dt As Date) As Boolean
Dim holidays As Variant
holidays = Array(#1/1/2023#, #4/3/2023#, #4/4/2023#, #5/1/2023#, #5/8/2023#, #5/22/2023#, #6/5/2023#, #7/14/2023#, #8/15/2023#, #11/1/2023#, #11/11/2023#, #12/25/2023#)
Dim i As Integer
For i = LBound(holidays) To UBound(holidays)
If DateValue(holidays(i)) = DateValue(dt) Then
Holiday = True
Exit Function
End If
Next i
Holiday = False
End Function |
Partager