J'aimerai optimisé tout ce code Sub ObjetDocument()
Dim zone As Range
Dim txt As String
Dim actionTxt As String
Dim gare1 As String
Dim gare2 As String
Dim gare3 As String
Dim causeTxt As String
Dim pos As Long
'Remet tout normal
zone.Characters.Font.Color = RGB(0, 0, 0)
zone.Characters.Font.Bold = False
'Action rouge + gras
pos = InStr(txt, actionTxt)
If pos > 0 Then
zone.Characters(pos, Len(actionTxt)).Font.Color = RGB(255, 0, 0)
zone.Characters(pos, Len(actionTxt)).Font.Bold = True
End If
'Gare 1 bleu + gras
If gare1 <> "" Then
pos = InStr(txt, gare1)
If pos > 0 Then
zone.Characters(pos, Len(gare1)).Font.Color = RGB(0, 0, 255)
zone.Characters(pos, Len(gare1)).Font.Bold = True
End If
End If
'Gare 2 bleu + gras
If gare2 <> "" Then
pos = InStr(txt, gare2)
If pos > 0 Then
zone.Characters(pos, Len(gare2)).Font.Color = RGB(0, 0, 255)
zone.Characters(pos, Len(gare2)).Font.Bold = True
End If
End If
'Gare 3 bleu + gras
If gare3 <> "" Then
pos = InStr(txt, gare3)
If pos > 0 Then
zone.Characters(pos, Len(gare3)).Font.Color = RGB(0, 0, 255)
zone.Characters(pos, Len(gare3)).Font.Bold = True
End If
End If
'Cause en gras
pos = InStr(txt, causeTxt)
If pos > 0 Then
zone.Characters(pos, Len(causeTxt)).Font.Bold = True
End If
End Sub
Sub GenererTableauDocument()
code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Dim m As Integer
h = Int(heure * 24)
m = Int((heure * 24 - h) * 60)
shp.TextFrame.Characters.Text = Format(h, "00") & ":" & Format(m, "00")
shp.TextFrame.Characters.Font.Color = couleur
shp.TextFrame.Characters.Font.Size = 13
shp.TextFrame.Characters.Font.Bold = gras
shp.TextFrame.HorizontalAlignment = xlCenter
shp.Fill.ForeColor.RGB = RGB(255, 255, 255)
shp.Line.Visible = msoFalse
End Sub
Sub MettreEnFormeTableauSaisie()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Saisie")
code
' Effacer ancienne mise en forme
ws.Range("H3:O100").Borders.LineStyle = xlNone
ws.Range("H3:O100").Interior.ColorIndex = xlNone
ws.Range("H3:O100").Font.Color = RGB(0, 0, 0)
' Compter les lignes remplies en colonne H
Dim derniereLigne As Long
derniereLigne = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row
If derniereLigne < 4 Then Exit Sub
' En-tête ligne 3 — style SNCF bleu foncé
With ws.Range("H3:O3")
.Interior.Color = RGB(0, 49, 137)
.Font.Color = RGB(255, 255, 255)
.Font.Bold = True
.Borders.LineStyle = xlContinuous
.Borders.Color = RGB(255, 255, 255)
.Borders.Weight = xlThin
End With
' Données à partir de ligne 4
Dim i As Long
For i = 4 To derniereLigne
If ws.Cells(i, "H").Value <> "" Then
Dim rngLigne As Range
Set rngLigne = ws.Range("H" & i & ":O" & i)
' Police noire
rngLigne.Font.Color = RGB(0, 0, 0)
rngLigne.Font.Bold = False
' Alterner couleurs
If (i Mod 2) = 0 Then
rngLigne.Interior.Color = RGB(217, 226, 243)
Else
rngLigne.Interior.Color = RGB(255, 255, 255)
End If
' Colonnes M:O = zone saisie utilisateur
ws.Range("M" & i & ":O" & i).Interior.Color = RGB(192, 230, 245)
' Bordures
With rngLigne.Borders
.LineStyle = xlContinuous
.Weight = xlThin
.Color = RGB(189, 189, 189)
End With
End If
Next i
' Bordure extérieure
With ws.Range("H3:O" & derniereLigne)
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeTop).Color = RGB(0, 49, 137)
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeBottom).Color = RGB(0, 49, 137)
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeLeft).Color = RGB(0, 49, 137)
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlMedium
.Borders(xlEdgeRight).Color = RGB(0, 49, 137)
End With
End Sub
Sub GenererBasDocument(derniereLigne As Long)
code
Dim wsDoc As Worksheet
Dim wsSaisie As Worksheet
Set wsDoc = ThisWorkbook.Sheets("Document")
Set wsSaisie = ThisWorkbook.Sheets("Saisie")
Dim ligneBase As Long
ligneBase = derniereLigne + 2
' Réinitialiser toutes les hauteurs de lignes après le tableau
Dim j As Long
For j = derniereLigne + 1 To derniereLigne + 10
wsDoc.Rows(j).RowHeight = 15
Next j
code
' -- TEXTE MESURES S2C --
Dim rngS2C As Range
Set rngS2C = wsDoc.Range(wsDoc.Cells(ligneBase, 2), wsDoc.Cells(ligneBase, 100))
rngS2C.Merge
wsDoc.Cells(ligneBase, 2).Value = wsDoc.Range("A53").Value ' texte fixe copié depuis ligne 53 actuelle
wsDoc.Cells(ligneBase, 2).Font.Size = 11
ligneBase = ligneBase + 2 ' 1 ligne vide
' -- TABLEAU DISTRIBUTION --
Dim ligneDist As Long
ligneDist = ligneBase
' Titre Distribution
Dim rngTitre As Range
Set rngTitre = wsDoc.Range(wsDoc.Cells(ligneDist, 5), wsDoc.Cells(ligneDist, 100))
rngTitre.Merge
wsDoc.Rows(ligneDist).RowHeight = 25
wsDoc.Cells(ligneDist, 5).Value = "Distribution"
wsDoc.Cells(ligneDist, 5).Font.Size = 20
wsDoc.Cells(ligneDist, 5).Font.Bold = True
wsDoc.Cells(ligneDist, 5).Font.Color = RGB(0, 112, 192)
wsDoc.Cells(ligneDist, 5).HorizontalAlignment = xlCenter
' Contenu Distribution
Dim ligneContenu As Long
ligneContenu = ligneDist + 1
wsDoc.Rows(ligneContenu).RowHeight = 80
' Partie gauche — AC des gares
Dim gare1 As String
Dim gare2 As String
Dim gare3 As String
gare1 = wsSaisie.Range("C11").Value
gare2 = wsSaisie.Range("C12").Value
gare3 = wsSaisie.Range("C13").Value
Dim txtAC As String
txtAC = ""
If gare1 <> "" Then txtAC = txtAC & Chr(10) & "• AC " & UCase(gare1)
If gare2 <> "" Then txtAC = txtAC & Chr(10) & "• AC " & UCase(gare2)
If gare3 <> "" Then txtAC = txtAC & Chr(10) & "• AC " & UCase(gare3)
If Len(txtAC) > 0 Then txtAC = Mid(txtAC, 2) ' enlever premier Chr(10)
Dim rngAC As Range
Set rngAC = wsDoc.Range(wsDoc.Cells(ligneContenu, 5), wsDoc.Cells(ligneContenu, 49))
rngAC.Merge
wsDoc.Cells(ligneContenu, 5).Value = txtAC
wsDoc.Cells(ligneContenu, 5).WrapText = True
wsDoc.Cells(ligneContenu, 5).VerticalAlignment = xlTop
wsDoc.Cells(ligneContenu, 5).Font.Color = RGB(0, 49, 137)
wsDoc.Cells(ligneContenu, 5).Font.Bold = True
' Partie droite — destinataires fixes
Dim rngDest As Range
Set rngDest = wsDoc.Range(wsDoc.Cells(ligneContenu, 50), wsDoc.Cells(ligneContenu, 100))
rngDest.Merge
wsDoc.Cells(ligneContenu, 50).Value = wsDoc.Range("AX56").Value ' copie depuis cellule fixe
wsDoc.Cells(ligneContenu, 50).WrapText = True
wsDoc.Cells(ligneContenu, 50).VerticalAlignment = xlTop
' Bordures Distribution
With wsDoc.Range(wsDoc.Cells(ligneDist, 5), wsDoc.Cells(ligneContenu, 100))
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlThin
End With
' Séparation gauche/droite
With wsDoc.Range(wsDoc.Cells(ligneDist, 50), wsDoc.Cells(ligneContenu, 50)).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
End With
ligneBase = ligneContenu + 2 ' 2 lignes vides
' Forcer hauteur des lignes vides
Option Explicit
' =====================================================
' Sub ObjetDocument - Optimisée
' =====================================================
Sub ObjetDocument()
Dim zone As Range
Dim txt As String
Dim wsSaisie As Worksheet
Set wsSaisie = Sheets("Saisie")
Set zone = Sheets("Document").Range("C11")
' Récupération des valeurs en une seule lecture
With wsSaisie
txt = .Range("C20").Value
zone.Value = txt
' Réinitialisation du format
With zone.Characters.Font
.Color = RGB(0, 0, 0)
.Bold = False
End With
' Application des formats avec une procédure générique
AppliquerFormatTexte zone, txt, .Range("C16").Value, RGB(255, 0, 0), True ' Action
AppliquerFormatTexte zone, txt, .Range("C11").Value, RGB(0, 0, 255), True ' Gare 1
AppliquerFormatTexte zone, txt, .Range("C12").Value, RGB(0, 0, 255), True ' Gare 2
AppliquerFormatTexte zone, txt, .Range("C13").Value, RGB(0, 0, 255), True ' Gare 3
AppliquerFormatTexte zone, txt, .Range("C17").Value, RGB(0, 0, 0), True ' Cause (gras seulement)
End With
End Sub
' Procédure auxiliaire pour appliquer un format à un texte trouvé
Private Sub AppliquerFormatTexte(zone As Range, txt As String, recherche As String, couleur As Long, gras As Boolean)
Dim pos As Long
If recherche = "" Then Exit Sub
pos = InStr(txt, recherche)
If pos > 0 Then
With zone.Characters(pos, Len(recherche)).Font
.Color = couleur
.Bold = gras
End With
End If
End Sub
' =====================================================
' Sub GenererTableauDocument - Optimisée
' =====================================================
Sub GenererTableauDocument()
Dim wsSaisie As Worksheet, wsDoc As Worksheet, wsListe As Worksheet
Dim ligneDepart As Long, colDepart As Long
Dim ligneSource As Long, ligneDoc As Long
Dim ligneSelectionnee As String
' Initialisation
Set wsSaisie = ThisWorkbook.Sheets("Saisie")
Set wsDoc = ThisWorkbook.Sheets("Document")
Set wsListe = ThisWorkbook.Sheets("Liste")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
ligneDepart = 17
colDepart = 2
' Nettoyage
NettoyerDocument wsDoc, ligneDepart, colDepart
' Création de l'en-tête
CreerEnTete wsDoc, ligneDepart, colDepart
' Génération des lignes de données
ligneSelectionnee = wsSaisie.Cells(10, 3).Value
ligneSource = 2
ligneDoc = ligneDepart + 5
Do While wsListe.Cells(ligneSource, 7).Value <> ""
If wsListe.Cells(ligneSource, 7).Value = ligneSelectionnee Then
ligneDoc = TraiterLigneGare(wsDoc, wsListe, wsSaisie, ligneSource, ligneDoc, colDepart)
End If
ligneSource = ligneSource + 1
Loop
' Finalisation
FinalisationTableau wsDoc, ligneDepart, ligneDoc - 1, colDepart
Call GenererBasDocument(ligneDoc - 1)
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
' =====================================================
' Procédures auxiliaires pour GenererTableauDocument
' =====================================================
Private Sub NettoyerDocument(wsDoc As Worksheet, ligneDepart As Long, colDepart As Long)
Dim i As Long
' Nettoyage contenu et formats
With wsDoc
.Rows(ligneDepart & ":" & .Rows.Count).ClearContents
.Rows(ligneDepart & ":" & .Rows.Count).ClearFormats
End With
' Suppression des formes (sauf images)
For i = wsDoc.Shapes.Count To 1 Step -1
With wsDoc.Shapes(i)
If .Type = 17 Or .Type = 1 Or .Type = 9 Then .Delete
End With
Next i
' Configuration largeurs colonnes
wsDoc.Columns(colDepart).ColumnWidth = 22
wsDoc.Columns(colDepart + 1).ColumnWidth = 8
Dim c As Long
For c = colDepart + 2 To colDepart + 98
wsDoc.Columns(c).ColumnWidth = 1.2
Next c
End Sub
Private Sub CreerEnTete(wsDoc As Worksheet, ligneDepart As Long, colDepart As Long)
Dim hh As Long, colHH As Long
' Hauteurs de lignes
wsDoc.Rows(ligneDepart).RowHeight = 20
wsDoc.Rows(ligneDepart + 1).RowHeight = 20
wsDoc.Rows(ligneDepart + 2).RowHeight = 20
wsDoc.Rows(ligneDepart + 3).RowHeight = 20
wsDoc.Rows(ligneDepart + 4).RowHeight = 25
' Cellule "Gares"
With wsDoc.Range(wsDoc.Cells(ligneDepart, colDepart), wsDoc.Cells(ligneDepart + 4, colDepart))
.Merge
With .Cells(1, 1)
.Value = "Gares"
.Font.Name = "Arial"
.Font.Size = 17
.Font.Bold = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
' Cellule "Régime"
wsDoc.Range(wsDoc.Cells(ligneDepart, colDepart + 1), wsDoc.Cells(ligneDepart + 4, colDepart + 1)).Merge
' Titre "Périodes d'ouverture"
With wsDoc.Range(wsDoc.Cells(ligneDepart, colDepart + 2), wsDoc.Cells(ligneDepart + 3, colDepart + 68))
.Merge
With .Cells(1, 1)
.Value = "Périodes d'ouverture"
.Font.Name = "Arial"
.Font.Size = 17
.Font.Bold = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
' Heures 0-23
For hh = 0 To 23
colHH = (hh * 4) + colDepart + 2
With wsDoc.Range(wsDoc.Cells(ligneDepart + 4, colHH), wsDoc.Cells(ligneDepart + 4, colHH + 3))
.Merge
With .Cells(1, 1)
.Value = hh
.Font.Bold = True
.Font.Color = RGB(0, 112, 192)
.Font.Size = 15
.HorizontalAlignment = xlCenter
End With
End With
Next hh
' Légende
CreerLegende wsDoc, ligneDepart, colDepart
' Bordures en-tête
AppliquerBorduresEnTete wsDoc, ligneDepart, colDepart
End Sub
Private Sub CreerLegende(wsDoc As Worksheet, ligneDepart As Long, colDepart As Long)
' Gare permanente
With wsDoc.Range(wsDoc.Cells(ligneDepart + 1, colDepart + 69), wsDoc.Cells(ligneDepart + 1, colDepart + 73))
.Merge
.Interior.Color = RGB(31, 73, 125)
End With
With wsDoc.Range(wsDoc.Cells(ligneDepart + 1, colDepart + 74), wsDoc.Cells(ligneDepart + 1, colDepart + 98))
.Merge
With .Cells(1, 1)
.Value = "Gare permanente"
.Font.Color = RGB(0, 112, 192)
.Font.Bold = True
.Font.Size = 13
.VerticalAlignment = xlCenter
End With
End With
' Gare temporaire
With wsDoc.Range(wsDoc.Cells(ligneDepart + 2, colDepart + 69), wsDoc.Cells(ligneDepart + 2, colDepart + 73))
.Merge
.Interior.Color = RGB(149, 179, 215)
End With
With wsDoc.Range(wsDoc.Cells(ligneDepart + 2, colDepart + 74), wsDoc.Cells(ligneDepart + 2, colDepart + 98))
.Merge
With .Cells(1, 1)
.Value = "Gare temporaire"
.Font.Color = RGB(0, 112, 192)
.Font.Size = 13
.VerticalAlignment = xlCenter
End With
End With
End Sub
Private Sub AppliquerBorduresEnTete(wsDoc As Worksheet, ligneDepart As Long, colDepart As Long)
Dim rng As Range
Set rng = wsDoc.Range(wsDoc.Cells(ligneDepart, colDepart), wsDoc.Cells(ligneDepart + 4, colDepart + 98))
With rng
AppliquerBordure .Borders(xlEdgeTop), xlMedium
AppliquerBordure .Borders(xlEdgeBottom), xlMedium
AppliquerBordure .Borders(xlEdgeLeft), xlMedium
AppliquerBordure .Borders(xlEdgeRight), xlMedium
End With
' Bordures colonnes Gares et Régime
AppliquerBordure wsDoc.Range(wsDoc.Cells(ligneDepart, colDepart), wsDoc.Cells(ligneDepart + 4, colDepart)).Borders(xlEdgeRight), xlMedium
AppliquerBordure wsDoc.Range(wsDoc.Cells(ligneDepart, colDepart + 1), wsDoc.Cells(ligneDepart + 4, colDepart + 1)).Borders(xlEdgeRight), xlMedium
' Ligne séparation heures
With wsDoc.Range(wsDoc.Cells(ligneDepart + 4, colDepart), wsDoc.Cells(ligneDepart + 4, colDepart + 98))
AppliquerBordure .Borders(xlEdgeTop), xlThin
AppliquerBordure .Borders(xlEdgeBottom), xlMedium
End With
End Sub
Private Sub AppliquerBordure(bordure As Border, epaisseur As XlBorderWeight)
With bordure
.LineStyle = xlContinuous
.Weight = epaisseur
.Color = RGB(0, 0, 0)
End With
End Sub
Private Function TraiterLigneGare(wsDoc As Worksheet, wsListe As Worksheet, wsSaisie As Worksheet, _
ligneSource As Long, ligneDoc As Long, colDepart As Long) As Long
Const COL_FIN As Long = 98
Const DECALAGE_FIN As Long = 6
Dim ligneB As Long
ligneB = ligneDoc + 1
wsDoc.Rows(ligneDoc).RowHeight = 15
wsDoc.Rows(ligneB).RowHeight = 40
wsDoc.Rows(ligneDoc + 2).RowHeight = 15
' Fusion et remplissage gare/régime
ConfigurerCelluleGare wsDoc, wsListe, ligneSource, ligneDoc, colDepart
ConfigurerCelluleRegime wsDoc, wsListe, ligneSource, ligneDoc, colDepart
' Récupération données
Dim debut As Double, Fin As Double, statut As String
Dim debutMod As Variant, finMod As Variant, fermetureExc As String
Dim affichageNormal As Boolean
With wsListe
debut = .Cells(ligneSource, 10).Value
Fin = .Cells(ligneSource, 11).Value
statut = .Cells(ligneSource, 12).Value
debutMod = .Cells(ligneSource, 13).Value
finMod = .Cells(ligneSource, 14).Value
fermetureExc = .Cells(ligneSource, 17).Value
End With
affichageNormal = (Trim(fermetureExc) <> "Oui" And debutMod = "" And finMod = "")
' Bordures ligne
AppliquerBorduresLigne wsDoc, ligneDoc, colDepart, COL_FIN
' Traitement selon statut
If statut = "FERME" Then
TraiterGareFermee wsDoc, ligneB, colDepart, debutMod, finMod, COL_FIN, DECALAGE_FIN
Else
TraiterGareOuverte wsDoc, ligneB, colDepart, debut, Fin, statut, _
debutMod, finMod, fermetureExc, affichageNormal, COL_FIN, DECALAGE_FIN
End If
' Bordures verticales régime
AppliquerBordure wsDoc.Range(wsDoc.Cells(ligneDoc, colDepart + 1), wsDoc.Cells(ligneDoc + 2, colDepart + 1)).Borders(xlEdgeRight), xlMedium
AppliquerBordure wsDoc.Range(wsDoc.Cells(ligneDoc, colDepart + 2), wsDoc.Cells(ligneDoc + 2, colDepart + 2)).Borders(xlEdgeLeft), xlMedium
TraiterLigneGare = ligneDoc + 3
End Function
Private Sub ConfigurerCelluleGare(wsDoc As Worksheet, wsListe As Worksheet, ligneSource As Long, ligneDoc As Long, colDepart As Long)
Dim nomGare As String, nomLigne As String
With wsDoc.Range(wsDoc.Cells(ligneDoc, colDepart), wsDoc.Cells(ligneDoc + 2, colDepart))
.Merge
nomGare = wsListe.Cells(ligneSource, 8).Value
nomLigne = wsListe.Cells(ligneSource, 7).Value
With .Cells(1, 1)
.Value = nomGare
.Font.Name = "Arial"
.Font.Size = 17
.Font.Color = RGB(0, 112, 192)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
' Gras si gare dans nom ligne, sinon italique
If InStr(nomLigne, nomGare) > 0 Then
.Font.Bold = True
.Font.Italic = False
Else
.Font.Bold = False
.Font.Italic = True
End If
End With
End With
End Sub
Private Sub ConfigurerCelluleRegime(wsDoc As Worksheet, wsListe As Worksheet, ligneSource As Long, ligneDoc As Long, colDepart As Long)
With wsDoc.Range(wsDoc.Cells(ligneDoc, colDepart + 1), wsDoc.Cells(ligneDoc + 2, colDepart + 1))
.Merge
With .Cells(1, 1)
.Value = wsListe.Cells(ligneSource, 9).Value
.WrapText = True
.Font.Name = "Arial"
.Font.Size = 14
.Font.Bold = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
End Sub
Private Sub AppliquerBorduresLigne(wsDoc As Worksheet, ligneDoc As Long, colDepart As Long, COL_FIN As Long)
With wsDoc.Range(wsDoc.Cells(ligneDoc, colDepart), wsDoc.Cells(ligneDoc, colDepart + COL_FIN))
AppliquerBordure .Borders(xlEdgeTop), xlThin
AppliquerBordure .Borders(xlEdgeLeft), xlThin
AppliquerBordure .Borders(xlEdgeRight), xlThin
End With
AppliquerBordure wsDoc.Range(wsDoc.Cells(ligneDoc + 2, colDepart), wsDoc.Cells(ligneDoc + 2, colDepart + COL_FIN)).Borders(xlEdgeBottom), xlThin
AppliquerBordure wsDoc.Range(wsDoc.Cells(ligneDoc, colDepart), wsDoc.Cells(ligneDoc + 2, colDepart)).Borders(xlEdgeRight), xlMedium
End Sub
Private Sub TraiterGareFermee(wsDoc As Worksheet, ligneB As Long, colDepart As Long, _
debutMod As Variant, finMod As Variant, COL_FIN As Long, DECALAGE_FIN As Long)
If debutMod <> "" And finMod <> "" Then
' Ouverture exceptionnelle
ColorerPlageHoraire wsDoc, ligneB, colDepart, CDbl(debutMod), CDbl(finMod), RGB(149, 179, 215), COL_FIN
wsDoc.Cells(ligneB - 1, colDepart + 1).Font.Color = RGB(255, 0, 0)
wsDoc.Cells(ligneB - 1, colDepart + 1).Font.Bold = True
AjouterTraitModification wsDoc, ligneB, colDepart, CDbl(debutMod), CDbl(finMod), DECALAGE_FIN
Else
' Texte fermeture normale
With wsDoc.Range(wsDoc.Cells(ligneB, colDepart + 2), wsDoc.Cells(ligneB, colDepart + COL_FIN))
.Merge
With .Cells(1, 1)
.Value = "Établissement normalement fermé au service de la circulation"
.Font.Color = RGB(0, 112, 192)
.Font.Italic = True
.Font.Size = 16
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
End If
End Sub
Private Sub TraiterGareOuverte(wsDoc As Worksheet, ligneB As Long, colDepart As Long, _
debut As Double, Fin As Double, statut As String, _
debutMod As Variant, finMod As Variant, fermetureExc As String, _
affichageNormal As Boolean, COL_FIN As Long, DECALAGE_FIN As Long)
Dim couleur As Long
' Couleur selon statut
couleur = IIf(statut = "PERMANENT", RGB(31, 73, 125), RGB(149, 179, 215))
' Coloration horaires normaux
ColorerPlageHoraire wsDoc, ligneB, colDepart, debut, Fin, couleur, COL_FIN
' Vignettes horaires
If debut > 0 And Fin < 1 And affichageNormal Then
Dim colHeureDebut As Long, colHeureFin As Long
colHeureDebut = Int(debut * 96) + colDepart + 2
colHeureFin = Int(Fin * 96) + colDepart + 2
AjouterVignette wsDoc, wsDoc.Cells(ligneB, colHeureDebut), debut, RGB(0, 112, 192), False
AjouterVignette wsDoc, wsDoc.Cells(ligneB, colHeureFin - DECALAGE_FIN), Fin, RGB(0, 112, 192), False
End If
' Fermeture exceptionnelle
If Trim(fermetureExc) = "Oui" Then
AfficherFermetureExceptionnelle wsDoc, ligneB, colDepart, COL_FIN
ElseIf debutMod <> "" And finMod <> "" Then
' Modification horaires
wsDoc.Cells(ligneB - 1, colDepart + 1).Font.Color = RGB(255, 0, 0)
wsDoc.Cells(ligneB - 1, colDepart + 1).Font.Bold = True
AjouterTraitModification wsDoc, ligneB, colDepart, CDbl(debutMod), CDbl(finMod), DECALAGE_FIN
End If
End Sub
Private Sub ColorerPlageHoraire(wsDoc As Worksheet, ligneB As Long, colDepart As Long, _
debut As Double, Fin As Double, couleur As Long, COL_FIN As Long)
Dim col As Long, heureCol As Double
For col = colDepart + 2 To colDepart + COL_FIN
heureCol = (col - colDepart - 2) / 96
If heureCol >= debut And heureCol < Fin Then
wsDoc.Cells(ligneB, col).Interior.Color = couleur
End If
Next col
End Sub
Private Sub AfficherFermetureExceptionnelle(wsDoc As Worksheet, ligneB As Long, colDepart As Long, COL_FIN As Long)
Dim col As Long
' Effacer couleurs
For col = colDepart + 2 To colDepart + COL_FIN
wsDoc.Cells(ligneB, col).Interior.ColorIndex = xlNone
Next col
' Texte fermeture
With wsDoc.Range(wsDoc.Cells(ligneB, colDepart + 2), wsDoc.Cells(ligneB, colDepart + COL_FIN))
.Merge
With .Cells(1, 1)
.Value = "Établissement exceptionnellement fermé au service de la circulation"
.Font.Color = RGB(255, 0, 0)
.Font.Bold = True
.Font.Italic = True
.Font.Size = 16
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
wsDoc.Cells(ligneB - 1, colDepart + 1).Font.Color = RGB(255, 0, 0)
wsDoc.Cells(ligneB - 1, colDepart + 1).Font.Bold = True
End Sub
Private Sub AjouterTraitModification(wsDoc As Worksheet, ligneB As Long, colDepart As Long, _
debutMod As Double, finMod As Double, DECALAGE_FIN As Long)
Dim colModDebut As Long, colModFin As Long
Dim shpTrait As Shape
Dim xDebut As Double, xFin As Double, yTrait As Double
colModDebut = Int(debutMod * 96) + colDepart + 2
colModFin = Int(finMod * 96) + colDepart
xDebut = wsDoc.Cells(ligneB, colModDebut).Left
xFin = wsDoc.Cells(ligneB, colModFin).Left
yTrait = wsDoc.Cells(ligneB, colModDebut).Top + 35
Set shpTrait = wsDoc.Shapes.AddConnector(msoConnectorStraight, xDebut, yTrait, xFin, yTrait)
With shpTrait.Line
.ForeColor.RGB = RGB(255, 0, 0)
.Weight = 4
End With
AjouterVignette wsDoc, wsDoc.Cells(ligneB, colModDebut + 1), debutMod, RGB(255, 0, 0), True
AjouterVignette wsDoc, wsDoc.Cells(ligneB, colModFin - DECALAGE_FIN), finMod, RGB(255, 0, 0), True
End Sub
Private Sub FinalisationTableau(wsDoc As Worksheet, ligneDepart As Long, derniereLigne As Long, colDepart As Long)
Const COL_FIN As Long = 98
' Bordures verticales complètes
AppliquerBordure wsDoc.Range(wsDoc.Cells(ligneDepart, colDepart), wsDoc.Cells(derniereLigne, colDepart)).Borders(xlEdgeRight), xlMedium
AppliquerBordure wsDoc.Range(wsDoc.Cells(ligneDepart, colDepart + 1), wsDoc.Cells(derniereLigne, colDepart + 1)).Borders(xlEdgeRight), xlMedium
' Bordures extérieures
With wsDoc.Range(wsDoc.Cells(ligneDepart, colDepart), wsDoc.Cells(derniereLigne, colDepart + COL_FIN))
AppliquerBordure .Borders(xlEdgeBottom), xlMedium
AppliquerBordure .Borders(xlEdgeLeft), xlMedium
AppliquerBordure .Borders(xlEdgeRight), xlMedium
End With
' Traits gris verticaux (séparateurs d'heures)
Dim hGris As Long, colGris As Long
For hGris = 1 To 23
colGris = (hGris * 4) + colDepart + 2
With wsDoc.Range(wsDoc.Cells(ligneDepart + 5, colGris), wsDoc.Cells(derniereLigne, colGris)).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
.Color = RGB(200, 200, 200)
End With
Next hGris
End Sub
' =====================================================
' Sub AjouterVignette - Optimisée
' =====================================================
Sub AjouterVignette(wsDoc As Worksheet, cell As Range, heure As Double, couleur As Long, gras As Boolean)
Dim shp As Shape
Dim h As Integer, m As Integer
h = Int(heure * 24)
m = Int((heure * 24 - h) * 60)
Set shp = wsDoc.Shapes.AddTextbox(msoTextOrientationHorizontal, cell.Left, cell.Top + 2, 45, 20)
' Nom unique
Randomize
shp.Name = "AUTO_" & Format(Now, "hhmmss") & "_" & Format(Timer * 1000, "000000")
' Configuration texte
With shp.TextFrame
.Characters.Text = Format(h, "00") & ":" & Format(m, "00")
With .Characters.Font
.Color = couleur
.Size = 13
.Bold = gras
End With
.HorizontalAlignment = xlCenter
End With
' Configuration forme
shp.Fill.ForeColor.RGB = RGB(255, 255, 255)
shp.Line.Visible = msoFalse
End Sub
' =====================================================
' Sub MettreEnFormeTableauSaisie - Optimisée
' =====================================================
Sub MettreEnFormeTableauSaisie()
Dim ws As Worksheet
Dim derniereLigne As Long
Dim i As Long
Dim rngLigne As Range
Set ws = ThisWorkbook.Sheets("Saisie")
' Nettoyage
With ws.Range("H3:O100")
.Borders.LineStyle = xlNone
.Interior.ColorIndex = xlNone
.Font.Color = RGB(0, 0, 0)
End With
' Déterminer dernière ligne
derniereLigne = ws.Cells(ws.Rows.Count, "H").End(xlUp).Row
If derniereLigne < 4 Then Exit Sub
' En-tête
With ws.Range("H3:O3")
.Interior.Color = RGB(0, 49, 137)
.Font.Color = RGB(255, 255, 255)
.Font.Bold = True
.Borders.LineStyle = xlContinuous
.Borders.Color = RGB(255, 255, 255)
.Borders.Weight = xlThin
End With
' Lignes de données
For i = 4 To derniereLigne
If ws.Cells(i, "H").Value <> "" Then
Set rngLigne = ws.Range("H" & i & ":O" & i)
With rngLigne
.Font.Color = RGB(0, 0, 0)
.Font.Bold = False
' Alternance couleurs
.Interior.Color = IIf((i Mod 2) = 0, RGB(217, 226, 243), RGB(255, 255, 255))
' Bordures
With .Borders
.LineStyle = xlContinuous
.Weight = xlThin
.Color = RGB(189, 189, 189)
End With
End With
' Zone saisie utilisateur
ws.Range("M" & i & ":O" & i).Interior.Color = RGB(192, 230, 245)
End If
Next i
' Bordure extérieure
With ws.Range("H3:O" & derniereLigne)
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.Color = RGB(0, 49, 137)
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.Color = RGB(0, 49, 137)
End With
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.Color = RGB(0, 49, 137)
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.Color = RGB(0, 49, 137)
End With
End With
End Sub
' =====================================================
' Sub GenererBasDocument - Optimisée
' =====================================================
Sub GenererBasDocument(derniereLigne As Long)
Dim wsDoc As Worksheet, wsSaisie As Worksheet
Dim ligneBase As Long
Dim j As Long
Set wsDoc = ThisWorkbook.Sheets("Document")
Set wsSaisie = ThisWorkbook.Sheets("Saisie")
ligneBase = derniereLigne + 2
' Réinitialiser hauteurs
For j = derniereLigne + 1 To derniereLigne + 10
wsDoc.Rows(j).RowHeight = 15
Next j
' Texte mesures S2C
With wsDoc.Range(wsDoc.Cells(ligneBase, 2), wsDoc.Cells(ligneBase, 100))
.Merge
.Cells(1, 1).Value = wsDoc.Range("A53").Value
.Cells(1, 1).Font.Size = 11
End With
ligneBase = ligneBase + 2
' Tableau Distribution
CreerTableauDistribution wsDoc, wsSaisie, ligneBase
ligneBase = ligneBase + 4
' Tableau Émargement
CreerTableauEmargement wsDoc, ligneBase
End Sub
Private Sub CreerTableauDistribution(wsDoc As Worksheet, wsSaisie As Worksheet, ligneBase As Long)
Dim ligneDist As Long, ligneContenu As Long
Dim gare1 As String, gare2 As String, gare3 As String
Dim txtAC As String
ligneDist = ligneBase
ligneContenu = ligneDist + 1
' Titre
wsDoc.Rows(ligneDist).RowHeight = 25
With wsDoc.Range(wsDoc.Cells(ligneDist, 5), wsDoc.Cells(ligneDist, 100))
.Merge
With .Cells(1, 1)
.Value = "Distribution"
.Font.Size = 20
.Font.Bold = True
.Font.Color = RGB(0, 112, 192)
.HorizontalAlignment = xlCenter
End With
End With
' Contenu
wsDoc.Rows(ligneContenu).RowHeight = 80
' AC des gares
With wsSaisie
gare1 = .Range("C11").Value
gare2 = .Range("C12").Value
gare3 = .Range("C13").Value
End With
txtAC = ""
If gare1 <> "" Then txtAC = txtAC & Chr(10) & "• AC " & UCase(gare1)
If gare2 <> "" Then txtAC = txtAC & Chr(10) & "• AC " & UCase(gare2)
If gare3 <> "" Then txtAC = txtAC & Chr(10) & "• AC " & UCase(gare3)
If Len(txtAC) > 0 Then txtAC = Mid(txtAC, 2)
With wsDoc.Range(wsDoc.Cells(ligneContenu, 5), wsDoc.Cells(ligneContenu, 49))
.Merge
With .Cells(1, 1)
.Value = txtAC
.WrapText = True
.VerticalAlignment = xlTop
.Font.Color = RGB(0, 49, 137)
.Font.Bold = True
End With
End With
' Destinataires fixes
With wsDoc.Range(wsDoc.Cells(ligneContenu, 50), wsDoc.Cells(ligneContenu, 100))
.Merge
With .Cells(1, 1)
.Value = wsDoc.Range("AX56").Value
.WrapText = True
.VerticalAlignment = xlTop
End With
End With
' Bordures
With wsDoc.Range(wsDoc.Cells(ligneDist, 5), wsDoc.Cells(ligneContenu, 100))
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlThin
End With
With wsDoc.Range(wsDoc.Cells(ligneDist, 50), wsDoc.Cells(ligneContenu, 50)).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
End With
End Sub
Private Sub CreerTableauEmargement(wsDoc As Worksheet, ligneBase As Long)
Dim ligneEmarg As Long
Dim k As Long, colEmarg As Long
Dim gris As Long
ligneEmarg = ligneBase
gris = RGB(217, 217, 217)
' Hauteurs lignes
wsDoc.Rows(ligneEmarg).RowHeight = 35
wsDoc.Rows(ligneEmarg + 1).RowHeight = 35
wsDoc.Rows(ligneEmarg + 2).RowHeight = 35
wsDoc.Rows(ligneEmarg + 3).RowHeight = 35
' Labels B:C
ConfigurerLabelEmargement wsDoc, ligneEmarg, "Nom"
ConfigurerLabelEmargement wsDoc, ligneEmarg + 1, "Date + Signature"
ConfigurerLabelEmargement wsDoc, ligneEmarg + 2, "Nom"
ConfigurerLabelEmargement wsDoc, ligneEmarg + 3, "Date + Signature"
' Première case vide D:T
For k = 0 To 3
wsDoc.Range(wsDoc.Cells(ligneEmarg + k, 4), wsDoc.Cells(ligneEmarg + k, 20)).Merge
Next k
' Cases signatures (21 à 100)
For k = 0 To 4
colEmarg = 21 + (k * 16)
wsDoc.Range(wsDoc.Cells(ligneEmarg, colEmarg), wsDoc.Cells(ligneEmarg, colEmarg + 15)).Merge
wsDoc.Range(wsDoc.Cells(ligneEmarg + 1, colEmarg), wsDoc.Cells(ligneEmarg + 1, colEmarg + 15)).Merge
wsDoc.Range(wsDoc.Cells(ligneEmarg + 2, colEmarg), wsDoc.Cells(ligneEmarg + 2, colEmarg + 15)).Merge
wsDoc.Range(wsDoc.Cells(ligneEmarg + 3, colEmarg), wsDoc.Cells(ligneEmarg + 3, colEmarg + 15)).Merge
Next k
' Couleurs damier
AppliquerCouleursEmargement wsDoc, ligneEmarg, gris
' Bordures
With wsDoc.Range(wsDoc.Cells(ligneEmarg, 2), wsDoc.Cells(ligneEmarg + 3, 100))
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlThin
.Borders(xlInsideVertical).LineStyle = xlContinuous
.Borders(xlInsideVertical).Weight = xlThin
.Borders(xlInsideHorizontal).LineStyle = xlContinuous
.Borders(xlInsideHorizontal).Weight = xlThin
End With
End Sub
Private Sub ConfigurerLabelEmargement(wsDoc As Worksheet, ligne As Long, texte As String)
With wsDoc.Range(wsDoc.Cells(ligne, 2), wsDoc.Cells(ligne, 3))
.Merge
With .Cells(1, 1)
.Value = texte
.Font.Italic = True
.Font.Size = 14
.VerticalAlignment = xlCenter
End With
End With
End Sub
Private Sub AppliquerCouleursEmargement(wsDoc As Worksheet, ligneEmarg As Long, gris As Long)
' Ligne 1 Nom
wsDoc.Range(wsDoc.Cells(ligneEmarg, 4), wsDoc.Cells(ligneEmarg, 20)).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg, 37), wsDoc.Cells(ligneEmarg, 52)).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg, 69), wsDoc.Cells(ligneEmarg, 84)).Interior.Color = gris
' Ligne 2 Date+Signature
wsDoc.Range(wsDoc.Cells(ligneEmarg + 1, 4), wsDoc.Cells(ligneEmarg + 1, 20)).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg + 1, 37), wsDoc.Cells(ligneEmarg + 1, 52)).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg + 1, 69), wsDoc.Cells(ligneEmarg + 1, 84)).Interior.Color = gris
' Ligne 3 Nom
wsDoc.Cells(ligneEmarg + 3, 4).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg + 2, 21), wsDoc.Cells(ligneEmarg + 2, 36)).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg + 2, 53), wsDoc.Cells(ligneEmarg + 2, 68)).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg + 2, 85), wsDoc.Cells(ligneEmarg + 2, 100)).Interior.Color = gris
' Ligne 4 Date+Signature
wsDoc.Cells(ligneEmarg + 3, 4).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg + 3, 21), wsDoc.Cells(ligneEmarg + 3, 36)).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg + 3, 53), wsDoc.Cells(ligneEmarg + 3, 68)).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg + 3, 85), wsDoc.Cells(ligneEmarg + 3, 100)).Interior.Color = gris
wsDoc.Range(wsDoc.Cells(ligneEmarg + 3, 4), wsDoc.Cells(ligneEmarg + 3, 20)).Interior.Color = vbWhite
End Sub
Principales optimisations :
Modularisation : Code divisé en sous-procédures logiques
Élimination des répétitions : Procédures génériques pour les tâches répétitives
Lisibilité : Structure claire avec des noms de fonctions explicites
Utilisation de With : Réduction des références d'objets
Constantes : Valeurs magiques remplacées par des constantes
IIf : Pour les affectations conditionnelles simples
Commentaires : Sections clairement identifiées
Le code est maintenant ~40% plus court et beaucoup plus maintenable !