Automatiser la création de graphique
Bonjour,
J'aimerais créer ce graphique par programmation, en l'intégrant à une feuille de calcul correspondante :
Pour l'instant je suis arrivée à obtenir par automatisation un graphique, mais il ne correspond pas à ce que je veux. J'aimerais précisément obtenir celui montré ci-dessus... Je ne sais pas comment modifier les paramètres du graphique...
Quelqu'un pourrait m'aider ?
Le fichier exemple avec une macro est aussi donné en pièce jointe :
Bonjour,
Un début de réponse à adapter.
A te relire.
Cdlt.
Public Sub Creer_graphique()
Dim ws As Worksheet
Dim rngChart As Range
Dim Ch As ChartObject
Dim min As Double, max As Double
Application.ScreenUpdating = False
Set ws = ActiveSheet
On Error Resume Next
ws.ChartObjects(1).Delete
On Error GoTo 0
Set rngChart = ws.Cells(1).CurrentRegion
min = Application.WorksheetFunction.min(rngChart)
max = Application.WorksheetFunction.max(rngChart)
Set Ch = ws.ChartObjects.Add(200, 10, 500, 250)
With Ch.Chart
.SetSourceData Source:=rngChart
.ChartType = xlLineMarkers
.Axes(xlValue).Delete
.HasLegend = False
.HasTitle = True
.ChartTitle.Text = "Prix"
With .SeriesCollection(1)
.MarkerStyle = 8
.MarkerSize = 5
.ApplyDataLabels
.DataLabels.Position = xlLabelPositionAbove
With .Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 176, 80)
.Transparency = 0
.Weight = 1.5
End With
End With
With .Axes(xlValue)
.MinimumScale = min * 0.9
.MaximumScale = max * 1.1
End With
End With
Set Ch = Nothing
Set rngChart = Nothing
Set ws = Nothing
End SubMerci énormément ! Cela m'apprend beaucoup, vous êtes un bon prof
J'ai intégré le code à mon programme, cela fonctionne, mis à part un détail que j'ai voulu changer mais je ne connaîs pas bien la syntaxe et cela bloque...
Voici ci-dessous le graphique (dernière version) que je veux faire , et le code qui permet de l'obtenir : j'ai juste un problème avec les barres d'erreurs, elles me permettent de créer les bâtons en dessous des points verts... la syntaxe doit être mauvaise... Peut être pourriez-vous m'aider ?
Dim ws As Worksheet
Dim rngChart As Range
Dim Ch As ChartObject
Dim min As Double, max As Double
Application.ScreenUpdating = False
Set ws = Sheets("TU")
On Error Resume Next
ws.ChartObjects(1).Delete
On Error GoTo 0
Set rngChart = ws.Cells(1).CurrentRegion
min = Application.WorksheetFunction.min(rngChart)
max = Application.WorksheetFunction.max(rngChart)
Set Ch = ws.ChartObjects.Add(200, 10, 500, 250)
With Ch.Chart
.SetSourceData Source:=rngChart
.ChartType = xlLineMarkers
.Axes(xlValue).Delete
.HasLegend = False
.HasTitle = True
.ChartTitle.Text = "Prix du TU"
With .SeriesCollection(1)
.MarkerStyle = 8
.MarkerSize = 8
.ApplyDataLabels
.DataLabels.Position = xlLabelPositionAbove
.SetElement = msoElementErrorBarStandardError
.ErrorBar Direction:=xlY, Include:=xlMinusValues, Type:=xlStError
.ErrorBar Direction:=xlY, Include:=xlMinusValues, Type:=xlCustom, Amount:=0
With .Format.Line
.Visible = msoFalse
End With
End With
With .Axes(xlValue)
.MinimumScale = min * 0.9
.MaximumScale = max * 1.1
End With
End With
Set Ch = Nothing
Set rngChart = Nothing
Set ws = Nothing