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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
Option Explicit
Private Sub test_TraiterToutesDonnees()
Call TraiterToutesDonnees(ActiveWorkbook.Name)
End Sub
Public Sub TraiterToutesDonnees(prmNomWorkbook As String)
Dim wb As Workbook: Set wb = Workbooks(prmNomWorkbook)
Dim sParam As Worksheet
Dim nomDonnees As String
Dim titreGraphique As String
Dim titreAxeY As String
Dim s As Worksheet: For Each s In wb.Worksheets
If s.Name Like "D_*" Then
nomDonnees = Mid(s.Name, 3)
Set sParam = wb.Worksheets("P_" & nomDonnees)
titreGraphique = lireParam(sParam, "TitreGraphique")
titreAxeY = lireParam(sParam, "TitreAxeY")
Call CreerGraphique(wb.Name, s.Name, "G_" & Mid(s.Name, 3), titreGraphique, titreAxeY)
'Masquer la feuille de données
s.Visible = xlSheetHidden
'Masquer la feuille de paramêtre
sParam.Visible = xlSheetHidden
Set sParam = Nothing
End If
Next s
wb.Save
Set wb = Nothing
End Sub
Private Function lireParam(prmSheetParam As Worksheet, prmNomParam As String) As String
Dim result As String
Const COLONNE_NOM_PARAM As Integer = 1
Const COLONNE_VAL_PARAM As Integer = 2
Const I_LIGNE_DATA_MIN As Long = 2
Const I_LIGNE_DATA_MAX As Long = 65535
Dim iLigne As Long: For iLigne = I_LIGNE_DATA_MIN To I_LIGNE_DATA_MAX
If prmSheetParam.Cells(iLigne, COLONNE_NOM_PARAM) = "" Then
Error (5)
End If
If LCase(Trim(prmSheetParam.Cells(iLigne, COLONNE_NOM_PARAM))) = LCase(Trim(prmNomParam)) Then
result = prmSheetParam.Cells(iLigne, COLONNE_VAL_PARAM)
Exit For
End If
Next iLigne
lireParam = result
End Function
Private Sub Test_CreerGraphique()
Call CreerGraphique(ActiveWorkbook.Name, "D_Int_Heure", "G_Int_Heure", "Interventions par heures", "Interventions")
End Sub
Private Sub CreerGraphique( _
prmNomWorkbook As String, _
prmNomDonnees As String, _
prmNomGraphique As String, _
prmTitreGraphique As String, _
prmTitreAxeY As String)
Dim nomDonnees As String: nomDonnees = prmNomDonnees
Dim nomGraphique As String: nomGraphique = prmNomGraphique
Dim titreGraphique As String: titreGraphique = prmTitreGraphique
Dim titreAxeY As String: titreAxeY = prmTitreAxeY
Workbooks(prmNomWorkbook).Activate
Dim sData As Worksheet: Set sData = ActiveWorkbook.Sheets(nomDonnees)
Const I_LIGNE_DATA_LEGENDE_DONNEES As Long = 1
Const I_LIGNE_DATA_MIN As Long = 2
Const I_LIGNE_DATA_MAX As Long = 65535
Const I_COLONNE_DATA_LEGENDE_SERIE As Integer = 1
Const I_COLONNE_DATA_MIN As Integer = 2
Const I_COLONNE_DATA_MAX As Integer = 255
'Analyse les données pour trouver les séries
Dim iLigne As Long
Dim iColonne As Integer
Dim nbSerie As Integer
Dim nbDonnees As Integer
Dim wb As Workbook: Set wb = Workbooks(prmNomWorkbook)
wb.Activate
sData.Activate
nbSerie = 0
For iLigne = I_LIGNE_DATA_MIN To I_LIGNE_DATA_MAX
If sData.Range(Cells(iLigne, I_COLONNE_DATA_LEGENDE_SERIE), Cells(iLigne, I_COLONNE_DATA_LEGENDE_SERIE)) = "" Then
Exit For
End If
nbSerie = nbSerie + 1
Next iLigne
Debug.Print nbSerie
nbDonnees = 0
For iColonne = I_COLONNE_DATA_MIN To I_COLONNE_DATA_MAX
If sData.Range(Cells(I_LIGNE_DATA_LEGENDE_DONNEES, iColonne), Cells(I_LIGNE_DATA_LEGENDE_DONNEES, iColonne)) = "" Then
Exit For
End If
nbDonnees = nbDonnees + 1
Next iColonne
Debug.Print nbDonnees
'Créer le graphique correspondant
Dim c As Chart
For Each c In ActiveWorkbook.Charts
Debug.Print c.Name
If c.Name = nomGraphique Then
'Détruire la feuille existante
'Mettre une alerte avant le lancement du programme pour prévenir l'utilisateur
Application.DisplayAlerts = False
Call wb.Charts(c.Name).Delete
Application.DisplayAlerts = True
Exit For
End If
Next c
Charts.Add
Set c = ActiveChart
Dim iSerie As Integer
With c
.ChartType = xlColumnClustered
.SetSourceData Source:=Sheets(nomDonnees). _
Range(Sheets(nomDonnees).Cells(I_LIGNE_DATA_LEGENDE_DONNEES, I_COLONNE_DATA_LEGENDE_SERIE), _
Sheets(nomDonnees).Cells(nbSerie + 1, nbDonnees + 1)), _
PlotBy:=xlRows
'ActiveChart.SetSourceData Source:=Sheets("rStatRecap_Heure_NbIntervention").Range("B1:Y4"), PlotBy:=xlRows
For iSerie = 1 To nbSerie
.SeriesCollection(iSerie).Values = "=" & nomDonnees & "!R" & CStr(iSerie + 1) & "C" & CStr(I_COLONNE_DATA_MIN) & ":" & "R" & CStr(iSerie + 1) & "C" & CStr(nbDonnees + 1)
.SeriesCollection(iSerie).Name = "=" & nomDonnees & "!R" & CStr(iSerie + 1) & "C" & CStr(I_COLONNE_DATA_LEGENDE_SERIE)
Next iSerie
.Location Where:=xlLocationAsNewSheet, Name:=nomGraphique
'Titre du graphique et titre des axes
.HasTitle = True
.ChartTitle.Characters.Text = titreGraphique
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = titreAxeY
'Gère les légendes des barres
For iSerie = 1 To nbSerie
'ActiveChart.SeriesCollection(1).XValues = "=D_Int_Heure!R1C2:R1C25"
.SeriesCollection(iSerie).XValues = "=" & nomDonnees & "!R" & CStr(I_LIGNE_DATA_LEGENDE_DONNEES) & "C" & CStr(I_COLONNE_DATA_MIN) & ":" & "R" & CStr(I_LIGNE_DATA_LEGENDE_DONNEES) & "C" & CStr(nbDonnees + 1)
If iSerie <> nbSerie Then
.SeriesCollection(iSerie).ApplyDataLabels _
AutoText:=True, _
LegendKey:=False, _
ShowSeriesName:=False, _
ShowCategoryName:=False, _
ShowValue:=False, _
ShowPercentage:=False, _
ShowBubbleSize:=False
Else
.SeriesCollection(iSerie).ApplyDataLabels _
AutoText:=True, _
LegendKey:=False, _
ShowSeriesName:=False, _
ShowCategoryName:=False, _
ShowValue:=True, _
ShowPercentage:=False, _
ShowBubbleSize:=False
.SeriesCollection(iSerie).DataLabels.Select
With Selection.Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
End If
Next iSerie
'Ajuste la taille des polices
'Axe des X
.Axes(xlCategory).Select
'Taille de la police
Selection.TickLabels.AutoScaleFont = True
With Selection.TickLabels.Font
.Name = "Arial"
.FontStyle = "Normal"
.Size = 6
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
'Orientation du texte
With Selection.TickLabels
.Alignment = xlCenter
.Offset = 100
.ReadingOrder = xlContext
.Orientation = 45
End With
'Axe des Y
.Axes(xlValue).Select
Selection.TickLabels.AutoScaleFont = True
With Selection.TickLabels.Font
.Name = "Arial"
.FontStyle = "Normal"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
'Titre axe Y
.Axes(xlValue).AxisTitle.Select
Selection.AutoScaleFont = True
With Selection.Font
.Name = "Arial"
.FontStyle = "Gras"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
'Légende
.Legend.Select
Selection.AutoScaleFont = True
With Selection.Font
.Name = "Arial"
.FontStyle = "Normal"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
'Titre
.ChartTitle.Select
Selection.AutoScaleFont = True
With Selection.Font
.Name = "Arial"
.FontStyle = "Gras"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
End With
Set wb = Nothing
End Sub
Private Sub TestCreationGraphique()
'
' Macro4 Macro
' Macro enregistrée le 2009-10-15 par René Marot
'
'
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("rStatRecap_Heure_NbIntervention"). _
Range("B1:Y4"), PlotBy:=xlRows
ActiveChart.SeriesCollection(1).Values = _
"=rStatRecap_Heure_NbIntervention!R2C2:R2C25"
ActiveChart.SeriesCollection(1).Name = "=rStatRecap_Heure_NbIntervention!R2C1"
ActiveChart.SeriesCollection(2).Values = _
"=rStatRecap_Heure_NbIntervention!R3C2:R3C25"
ActiveChart.SeriesCollection(2).Name = "=rStatRecap_Heure_NbIntervention!R3C1"
ActiveChart.SeriesCollection(3).Values = _
"=rStatRecap_Heure_NbIntervention!R4C2:R4C25"
ActiveChart.SeriesCollection(3).Name = "=rStatRecap_Heure_NbIntervention!R4C1"
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:="Heure"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Interventions par heures par années"
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Interventions"
.SeriesCollection(1).ApplyDataLabels AutoText:=True, LegendKey:= _
False, ShowSeriesName:=False, ShowCategoryName:=False, ShowValue:=False, _
ShowPercentage:=False, ShowBubbleSize:=False
.SeriesCollection(2).ApplyDataLabels AutoText:=True, LegendKey:= _
False, ShowSeriesName:=False, ShowCategoryName:=False, ShowValue:=False, _
ShowPercentage:=False, ShowBubbleSize:=False
.SeriesCollection(3).ApplyDataLabels AutoText:=True, LegendKey:= _
False, ShowSeriesName:=False, ShowCategoryName:=False, ShowValue:=True, _
ShowPercentage:=False, ShowBubbleSize:=False
.SeriesCollection(3).DataLabels.Select
With Selection.Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.Background = xlAutomatic
End With
End With
End Sub |
Partager