Position Graphique VBA

Résolu/Fermé
choco_ben - 4 mai 2010 à 16:58
 choco_ben - 5 mai 2010 à 09:57
Voici mon problème :

Je souhaite mettre plusieurs graphiques sur une feuille Excel et ensuite les positionner comme je le souhaite. J'ai réussi comme je le souhaitais pour le premier mais lorsque je souhaite positionner le second, c'est le premier qui se déplace.
Je n'arrive pas à comprendre quel est le problème ...

Voici mon code :

'1er graphique

Range("A4:I4,A62:I62").Select
Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Sheets("Fiche Agence").Range( _
"A4:I4,A62:I62"), PlotBy:=xlRows
ActiveChart.Location Where:=xlLocationAsObject, Name:="Fiche Agence"
With ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Période"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Chiffre d'affaires en €"
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlValue, xlPrimary) = True
End With
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlCategoryScale
ActiveChart.HasLegend = True
ActiveChart.Legend.Select
Selection.Position = xlBottom
'compte le nombre de graphiques dans la feuille
nbgraph = Sheets("Fiche Agence").ChartObjects.Count
'le nouveau graphique correspond à l'index le plus élevé
Sheets("Fiche Agence").ChartObjects(nbgraph).Name = "CA"
With Sheets("Fiche Agence").ChartObjects("CA")
.Left = Range("A79").Left
.Top = Range("A79").Top
End With
ActiveSheet.ChartObjects("CA").Activate
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
With Selection.Interior
.ColorIndex = 2
.PatternColorIndex = 1
.Pattern = xlSolid
End With
ActiveChart.Axes(xlValue).MajorGridlines.Select
With Selection.Border
.ColorIndex = 57
.Weight = xlHairline
.LineStyle = xlDot
End With
ActiveChart.Axes(xlCategory).Select
With Selection.TickLabels
.Alignment = xlCenter
.Offset = 100
.ReadingOrder = xlContext
.Orientation = 45
End With

'2eme graphique

Range("A4:I4,A31:I31").Select
Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Sheets("Fiche Agence").Range( _
"A4:I4,A31:I31"), PlotBy:=xlRows
ActiveChart.Location Where:=xlLocationAsObject, Name:="Fiche Agence"
With ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Période"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Chiffre d'affaires en €"
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlValue, xlPrimary) = True
End With
ActiveChart.Axes(xlCategory, xlPrimary).CategoryType = xlCategoryScale
ActiveChart.HasLegend = True
ActiveChart.Legend.Select
Selection.Position = xlBottom
'compte le nombre de graphiques dans la feuille
nbgraph = Sheets("Fiche Agence").ChartObjects.Count
'le nouveau graphique correspond à l'index le plus élevé
Sheets("Fiche Agence").ChartObjects(nbgraph).Name = "CA-Epargne"
With Sheets("Fiche Agence").ChartObjects("CA-Epargne")
.Left = Range("D79").Left
.Top = Range("D79").Top
End With
ActiveSheet.ChartObjects("CA-Epargne").Activate
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
With Selection.Interior
.ColorIndex = 2
.PatternColorIndex = 1
.Pattern = xlSolid
End With
ActiveChart.Axes(xlValue).MajorGridlines.Select
With Selection.Border
.ColorIndex = 57
.Weight = xlHairline
.LineStyle = xlDot
End With
ActiveChart.Axes(xlCategory).Select
With Selection.TickLabels
.Alignment = xlCenter
.Offset = 100
.ReadingOrder = xlContext
.Orientation = 45
End With

A voir également:

2 réponses

lermite222 Messages postés 8724 Date d'inscription dimanche 8 avril 2007 Statut Contributeur Dernière intervention 22 janvier 2020 1 190
Modifié par lermite222 le 4/05/2010 à 17:26
Bonjour,
Tu ne doit pas positionner par rapport à une cellule mais avec des références aux bords de la feuille.
    ActiveSheet.Shapes("Graphique 1").Left = 100
    ActiveSheet.Shapes("Graphique 1").Top = 20

A+
L'expérience instruit plus sûrement que le conseil. (André Gide)
Si tu te cogne à un pot et que ça sonne creux, c'est pas forcément le pot qui est vide. ;-)(Confucius)
0
Non mais ça marche aussi avec les cellules étant donné que si j'execute uniquement le premier graphique ça fonctionne.

Mon problème est que lorsque j'execute le deuxième graphique, c'est le premier qui va dans la cellule D79 alors que ça devrait être le second ...
0
lermite222 Messages postés 8724 Date d'inscription dimanche 8 avril 2007 Statut Contributeur Dernière intervention 22 janvier 2020 1 190
4 mai 2010 à 20:36
Tu a probablement un On Error quelque part parce que la ligne..
    With Sheets("Fiche Agence").ChartObjects("CA-Epargne") 

Gérnère l'erreur 1004
Faut remplacer par
    With Sheets("Fiche Agence").ChartObjects(nbgraph ) 

Cher moi ça fonctionne comme ça !
A+
0
Je te remercie beaucoup ! ça fonctionne en effet !
0