VBA - Création graphiques multiples
Bonjour à tous,
J'ai fait un code VBA pour créer un graphique à partir de données dont la source est dans un tableau structuré (feuille DataSource, Tableau). Je souhaite tracer plusieurs courbes dans ce un graphique. Le graphique est de type xlXYScatterSmoothNoMarkers. Pour chaque courbe que je trace sur ce graphique, je définis la source pour les axes des X (avec .XValues) et l'axe des Y (avec .Values).
Mon problème est le suivant : Excel trace l'ensemble des données présentes dans DataSource sans que je lui demande ! J'ai essayé de réordonner mon code de différentes manières mais je ne vois pas comment faire... Je rate surement quelque chose à force de loucher sur mon code.
Est ce que quelqu'un aurait une solution ?
Je vous joins mon exemple en PJ (fichier "aide").
Merci d'avance pour votre aide.
Bonjour
Quand tu supprimes la feuille, la cellule active se trouve dans le Tableau qui est alors sélectionné comme source du Charts.Add
A adapter
Sub Graphique()
Const Chart_1 As String = "Chart 1" ' Déclaration des nom et type de graphique
Dim chGraph1 As Chart ' Déclaration des graphiques
Application.ScreenUpdating = False
On Error Resume Next ' Suppression des graphiques si déjà existant
Sheets(Chart_1).Delete
On Error GoTo 0
With [Tableau].ListObject
.ListColumns("Data_4").Range.Select
End With
Set chGraph1 = Charts.Add ' Création des graphiques. 1 graphique par feuille
With chGraph1.SeriesCollection.NewSeries
.Name = "Reajusted Crosshead 2"
.XValues = [Tableau].ListObject.ListColumns("Data_4").DataBodyRange
.Values = [Tableau].ListObject.ListColumns("Data_5").DataBodyRange
.Border.Color = RGB(167, 211, 255)
.Border.Weight = 3
End With
With chGraph1 ' Graphique 1
.HasTitle = True
.ChartTitle.Characters.Text = "CHART 1"
.ChartTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(60, 60, 60)
.Name = Chart_1
.Legend.Position = xlLegendPositionTop
.ChartType = xlXYScatterSmoothNoMarkers
'Axe X
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Axe 1"
.Axes(xlValue).AxisTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(60, 60, 60)
.Axes(xlValue).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 12
.Axes(xlValue, xlPrimary).TickLabels.NumberFormat = "0"
.Axes(xlValue, xlPrimary).MinimumScale = 0
With .Axes(xlValue).TickLabels.Font
.Bold = True
.Color = RGB(89, 89, 89)
.Size = 10
End With
'Axe Y
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Axe 2"
.Axes(xlCategory).AxisTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(60, 60, 60)
.Axes(xlCategory).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 12
.Axes(xlCategory, xlPrimary).MinimumScale = 0
.Axes(xlCategory, xlPrimary).MajorUnit = 2
.Axes(xlCategory, xlPrimary).TickLabels.NumberFormat = "0"
With .Axes(xlCategory).TickLabels.Font
.Bold = True
.Color = RGB(89, 89, 89)
.Size = 10
End With
End With
' With chGraph1.SeriesCollection.NewSeries
' .Name = "Reajusted Slope"
' .XValues = Range("Tableau[Data_6]")
' .Values = Range("Tableau[Data_7]")
' .Border.Color = RGB(68, 114, 196)
' .Border.Weight = 3
' End With
With chGraph1
.SetElement (msoElementPrimaryValueGridLinesMajor)
.SetElement (msoElementPrimaryCategoryGridLinesMajor)
.SetElement (msoElementPrimaryValueGridLinesMinorMajor)
.SetElement (msoElementPrimaryCategoryGridLinesMinorMajor)
End With
With chGraph1.Axes(xlValue)
.MajorGridlines.Border.Color = RGB(217, 217, 217)
.MinorGridlines.Border.Color = RGB(242, 242, 242)
End With
With chGraph1.Axes(xlCategory)
.MajorGridlines.Border.Color = RGB(217, 217, 217)
.MinorGridlines.Border.Color = RGB(242, 242, 242)
End With ' Fin du code pour Graphique 1
Application.ScreenUpdating = True
End Sub
Ah ok !! Je tourne en boucle mais la solution est simple. Merci 78Chris. Finalement je me suis juste placé dans ma feuille de source de données avec :
Sheets("DataSource").Range("Z1").Select
A plus !