Créer multiple graphique VBA
d
Bonjour, je cherche un moyen de créer plusieurs graphiques avec une boucle for. Merci d'avance
voici le code que j'utilise, il ne prend pas le graph(i) ;
For i = 1 To nb
Dim grap As ChartObject
Dim graph As Chart
Set grap(i) = Sheets("Duty cycle graph cycle ").ChartObjects.Add(Range("Q2").Offset(0, 30 * (i - 1)).Left, Range("Q2").Offset(0, 30 * (i - 1)).Top, 800, 400)
Set graph(i) = grap.Chart
With graph(i)
.HasTitle = True
.HasLegend = True
.ChartTitle.Text = "Hauteur des chevrons selon les différents caoutchouc " & Sheets("Étape 9-chevrons").Range("b5").Offset(0, i)
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "hauteur (pouce)"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "endroit de la chenille"
Set xx = Sheets("Étape 9-chevrons").Range("B9:b18")
For j = 1 To 6
Set yy = Sheets("Étape 9-chevrons").Range("c9:c18").Offset(9 * (j - 1), 0)
graph(i).SeriesCollection.NewSeries
graph(i).SeriesCollection(j).Name = Sheets("Étape 9-chevrons").Range("AJ8").Offset(j, 0)
graph(i).SeriesCollection(j).XValues = xx
graph(i).SeriesCollection(j).Values = yy
graph(i).SeriesCollection(j).ChartType = xlLineMarkers
Next j
End With
Next i
End SubBonjour,
essaie ainsi :
Public Sub Create_charts()
Dim wsChart As Worksheet, wsData As Worksheet
Dim objChart As ChartObject
Dim strTitle As String
Dim rCell As Range, X_axis As Range, Y_axis As Range
Dim i As Long, j As Long
Set wsChart = Worksheets("Duty cycle graph cycle")
Set wsData = Worksheets("?tape 9-chevrons")
For Each objChart In wsChart.ChartObjects
objChart.Delete
Next objChart
Set X_axis = wsData.Range("B9:B18")
For i = 1 To 4
strTitle = wsData.Cells(5, 2).Offset(, i)
Set rCell = wsChart.Cells(2, 17).Offset(, 30 * (i - 1))
Set objChart = wsChart.ChartObjects.Add(rCell.Left, rCell.Top, 800, 400)
objChart.Name = "Chart_" & i
With objChart.Chart
.HasTitle = True
.HasLegend = True
.ChartTitle.Text = "Hauteur des chevrons selon les diff?rents caoutchouc " & strTitle
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Text = "Hauteur (pouce)"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "Endroit de la chenille"
For j = 1 To 6
Set Y_axis = wsData.Range("C9:C18").Offset(9 * (j - 1))
.SeriesCollection.NewSeries
.SeriesCollection(j).Name = wsData.Cells(8, 36).Offset(j, 0)
.SeriesCollection(j).XValues = X_axis
.SeriesCollection(j).Values = Y_axis
.SeriesCollection(j).ChartType = xlLineMarkers
Next j
strTitle = ""
End With
Next i
End Subd
Bonjour,
essaie ainsi :
Public Sub Create_charts() Dim wsChart As Worksheet, wsData As Worksheet Dim objChart As ChartObject Dim strTitle As String Dim rCell As Range, X_axis As Range, Y_axis As Range Dim i As Long, j As Long Set wsChart = Worksheets("Duty cycle graph cycle") Set wsData = Worksheets("?tape 9-chevrons") For Each objChart In wsChart.ChartObjects objChart.Delete Next objChart Set X_axis = wsData.Range("B9:B18") For i = 1 To 4 strTitle = wsData.Cells(5, 2).Offset(, i) Set rCell = wsChart.Cells(2, 17).Offset(, 30 * (i - 1)) Set objChart = wsChart.ChartObjects.Add(rCell.Left, rCell.Top, 800, 400) objChart.Name = "Chart_" & i With objChart.Chart .HasTitle = True .HasLegend = True .ChartTitle.Text = "Hauteur des chevrons selon les diff?rents caoutchouc " & strTitle .Axes(xlValue).HasTitle = True .Axes(xlValue).AxisTitle.Text = "Hauteur (pouce)" .Axes(xlCategory).HasTitle = True .Axes(xlCategory).AxisTitle.Text = "Endroit de la chenille" For j = 1 To 6 Set Y_axis = wsData.Range("C9:C18").Offset(9 * (j - 1)) .SeriesCollection.NewSeries .SeriesCollection(j).Name = wsData.Cells(8, 36).Offset(j, 0) .SeriesCollection(j).XValues = X_axis .SeriesCollection(j).Values = Y_axis .SeriesCollection(j).ChartType = xlLineMarkers Next j strTitle = "" End With Next i End Sub
Ça l'a fonctionné, un gros merci !