Sub graph2()
'Elabore les graphiques OK/KO et répartition des anomalies spécifiques de façon automatique
Dim x, y ' compteur de colonne et de ligne
Dim ligne1, col1 ' ligne entete et 1ere colonne du tableau saisies par utilisateur
Dim v_ent(3), v_ok(3), v_ko(3), v_ent2(3) As Variant
Dim v_col_ent2 ' variables pour les données du tableau
Dim i, j 'compteur
Dim MyStr ' variable temporaire pour chaine
Dim nomfeuille ' variable nom de la feuille active
Dim v_num_ano(10) As Variant ' variable anomalie specifique
Dim v_deb_ano(10) As Variant
Dim v_fin_ano(10) As Variant
ligne1 = InputBox("Saisir la ligne des entêtes du tableau", "Saisie", 2)
y = Range("B" & Rows.Count).End(xlUp).Row ' fin des lignes
x = Cells(ligne1, Cells.Columns.Count).End(xlToLeft).Column 'fin des colonnes
nomfeuille = ActiveSheet.Name
i = 1
'on cherche les données des graphiques
While i < x + 1
If Cells(ligne1, i).Value = "Entité classée" Then 'graph 1
v_ent(1) = Cells(ligne1, i).Address
End If
If Cells(ligne1, i).Value = "% de dossiers KO" Then ' graph 1
v_ko(1) = Cells(ligne1, i).Address
End If
If Cells(ligne1, i).Value = "% de dossiers OK" Then ' graph 1
v_ok(1) = Cells(ligne1, i).Address
End If
If Cells(ligne1, i).Value = "Entité" Then ' graph 2
v_ent2(1) = Cells(ligne1, i).Address
v_col_ent2 = Cells(ligne1, i).Column
End If
i = i + 1
Wend
'on travaille les variables pour obtenir toutes les valeurs dont on aura besoin
'v_ent
j = Len(v_ent(1))
MyStr = InStr(2, v_ent(1), "$")
i = j - MyStr
v_ent(3) = Left(v_ent(1), MyStr) & y
v_ent(2) = Left(v_ent(1), MyStr) & Right(v_ent(1), i) + 1
'v_ko
j = Len(v_ko(1))
MyStr = InStr(2, v_ko(1), "$")
i = j - MyStr
v_ko(3) = Left(v_ko(1), MyStr) & y
v_ko(2) = Left(v_ko(1), MyStr) & Right(v_ko(1), i) + 1
'v_ok
j = Len(v_ok(1))
MyStr = InStr(2, v_ok(1), "$")
i = j - MyStr
v_ok(3) = Left(v_ok(1), MyStr) & y
v_ok(2) = Left(v_ok(1), MyStr) & Right(v_ok(1), i) + 1
'v_ent2
j = Len(v_ent2(1))
MyStr = InStr(2, v_ent2(1), "$")
i = j - MyStr
v_ent2(3) = Left(v_ent2(1), MyStr) & y
v_ent2(2) = Left(v_ent2(1), MyStr) & Right(v_ent2(1), i) + 1
ActiveSheet.Shapes.AddChart.Select
ActiveChart.Parent.Name = "Graphique1"
ActiveChart.ChartType = xlCylinderBarStacked100
'ActiveChart.SetSourceData Source:=Range("'Feuil1'!$C$2:$E$47")
ActiveChart.SetSourceData Source:=Range(v_ent(1) & ":" & v_ok(3))
' SERIE 1
'ActiveChart.SeriesCollection(1).XValues = "='Feuil1'!$C$2:$C$47"
ActiveChart.SeriesCollection(1).XValues = "=" & nomfeuille & "!" & v_ent(2) & ":" & v_ent(3)
'ActiveChart.SeriesCollection.Add _
'Source:=Range(v_ko(1) & ":" & v_ko(3))
ActiveChart.SeriesCollection(1).Name = "=" & nomfeuille & "!" & v_ko(1) 'entete de données
ActiveChart.SeriesCollection(1).Values = "=" & nomfeuille & "!" & v_ko(2) & ":" & v_ko(3) 'données
' SERIE 2
ActiveChart.SeriesCollection.Add _
Source:=Range(v_ok(1) & ":" & v_ok(3))
ActiveChart.SeriesCollection(2).Name = "=" & nomfeuille & "!" & v_ok(1) 'entetes des données
ActiveChart.SeriesCollection(2).Values = "=" & nomfeuille & "!" & v_ok(2) & ":" & v_ok(3) 'données
'Retraitement: couleur, étiquettes et perpective
ActiveChart.SeriesCollection(1).Interior.Color = RGB(255, 0, 0) 'Couleur
ActiveChart.SeriesCollection(1).HasDataLabels = True ' étiquettes de données
ActiveChart.SeriesCollection(2).Interior.Color = RGB(0, 176, 80)
ActiveChart.SeriesCollection(2).HasDataLabels = True
ActiveChart.ChartArea.Format.ThreeD.Perspective = False 'active la case axes à angles droit dans rotation
'Gestion de l'échelle du graph
ActiveSheet.ChartObjects("Graphique1").Select
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScaleIsAuto = False
ActiveChart.Axes(xlValue).MaximumScaleIsAuto = False
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 1
'Emplacement du graphique
ActiveSheet.ChartObjects("Graphique1").Left = Cells(1, x + 2).Left
ActiveSheet.ChartObjects("Graphique1").Top = Cells(1, x + 2).Top
'2è GRAPHIQUE
j = x - v_col_ent2 'nombre d'anomalies spécifiques
Range(v_ent2(1)).Select
i = 1
Do
ActiveCell.Offset(0, 1).Select 'on décale à la colonne à droite
v_num_ano(i) = ActiveCell.Address 'on récupère l'adresse
a = Len(v_num_ano(i))
MyStr = InStr(2, v_num_ano(i), "$")
b = a - MyStr
v_fin_ano(i) = Left(v_num_ano(i), MyStr) & y
v_deb_ano(i) = Left(v_num_ano(i), MyStr) & Right(v_num_ano(i), b) + 1
i = i + 1
Loop Until i = j + 1
i = i - 1
ActiveSheet.Shapes.AddChart.Select
ActiveChart.Parent.Name = "Graphique2"
'ActiveChart.SetSourceData Source:=Range("'Feuil1'!$C$2:$E$47")
ActiveChart.SetSourceData Source:=Range(v_ent2(1) & ":" & v_fin_ano(i))
ActiveChart.ChartType = xlCylinderBarStacked100
ActiveChart.SeriesCollection(1).XValues = "=" & nomfeuille & "!" & v_ent2(2) & ":" & v_ent2(3)
' SERIES (Anomalies spécifiques)
i = 1
Do
ActiveChart.SeriesCollection(i).Name = "=" & nomfeuille & "!" & v_num_ano(i) 'entete de données
ActiveChart.SeriesCollection(i).Values = "=" & nomfeuille & "!" & v_deb_ano(i) & ":" & v_fin_ano(i) 'données
ActiveChart.SeriesCollection(i).HasDataLabels = True
i = i + 1
Loop Until i = j + 1
ActiveChart.ChartArea.Format.ThreeD.Perspective = False
'Gestion de l'échelle du graph
ActiveSheet.ChartObjects("Graphique2").Select
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScaleIsAuto = False
ActiveChart.Axes(xlValue).MaximumScaleIsAuto = False
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MaximumScale = 1
'Emplacement du graphique
ActiveSheet.ChartObjects("Graphique2").Left = Cells(1, x + 10).Left
ActiveSheet.ChartObjects("Graphique2").Top = Cells(1, x + 10).Top
Cells(ligne1, x + 10).Select
End Sub