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
| Sub Graph()
Dim Plage As Range
Dim Courbe As ChartObject
Dim Lig As Long, LigFin As Long, Pos As Long
Dim ListeFeuille() As Variant
Dim Sh As Worksheet
Application.ScreenUpdating = False
ListeFeuille = Array("CA Jour", "HEC Jour")
For j = LBound(ListeFeuille) To UBound(ListeFeuille)
Set Sh = ThisWorkbook.Worksheets(ListeFeuille(j))
Sh.Activate
For Each Courbe In Sh.ChartObjects
Courbe.Delete
Next Courbe
' pour les 6 zones du tableau (de A à TOTAL)
For k = 0 To 5
' exclusion de E
If k <> 4 Then
With Sh
Pos = 62 'ligne de départ de la zone source du graphique
'efface la zone de source du graphique
.Range(.Cells(Pos - 1, 2 + (3 * k)), .Cells(Pos + 30, 4 + (3 * k))).ClearContents
'titre des axes
.Cells(Pos - 1, 3 + (3 * k)).Value = "Objectif"
.Cells(Pos - 1, 4 + (3 * k)).Value = "Réalisé"
Application.Calculation = xlManual
Lig = 11 'ligne de départ des données du tableau
LigFin = .Cells(Lig, 1).End(xlDown).Row ' et la ligne de fin
For i = Lig To LigFin
' si il y a un numéro de jour
' et qu'il est différent du précédent
' (= on est pas sur un weekend ou jour férié)
If .Cells(i, 1) <> "" _
And .Cells(i, 1) <> .Cells(i - 1, 1) Then
' écriture des données pour la source du graphique
.Cells(Pos, 2 + (3 * k)) = .Cells(i, 1)
.Cells(Pos, 3 + (3 * k)) = .Cells(i, 4 + (3 * k))
.Cells(Pos, 4 + (3 * k)) = .Cells(i, 5 + (3 * k))
' on remplace les erreurs par des 0
If .Cells(i, 4 + (3 * k)) = "#REF!" Then .Cells(i, 4 + (3 * k)) = 0
If .Cells(i, 4 + (3 * k)) = "#REF!" Then .Cells(i, 5 + (3 * k)) = 0
Pos = Pos + 1
End If
Next i
Application.Calculation = xlAutomatic
' la source du graphique
Set Plage = .Range(.Cells(61, 2 + (3 * k)), .Cells(.Cells(62, 2).End(xlDown).Row, 4 + (3 * k)))
.Shapes.AddChart(xlLine).Select
End With
With ActiveChart
.SetSourceData Plage
.SetElement (msoElementDataTableWithLegendKeys)
.SetElement (msoElementChartTitleAboveChart)
' le titre du graphique
.ChartTitle.Caption = Sh.Cells(5, 4 + (3 * k)).Value
' le positionnement des graphiques
' avec gestion de l'agencement (total, puis A, puis B, puis C, puis D)
Set Plage = Sh.Range(Sh.Cells(43 + (18 * ((k + 1) Mod 6)), 2), Sh.Cells(60 + (18 * ((k + 1) Mod 6)), 18))
With .Parent
.Left = Plage.Left
.Width = Plage.Width
.Top = Plage.Top
.Height = Plage.Height
End With
End With
End If
Next k
Set Sh = Nothing
Next j
Application.ScreenUpdating = True
MsgBox "Terminé"
End Sub |
Partager