VBA définir le nom du graphique lors de l'ajout

ludoxa

XLDnaute Nouveau
Bonjour à tous,

j'exécute plusieurs macros d'ajout de graph sur un classeur. A chaque fois le nom du graphique s'incrémente "Graphique 1", "Graphique 2", etc... et j'utilise ce nom pour renommer et placer mon graph, voici le code :
' MISE EN FORME renommer le graphique pour pouvoir le placer
ActiveSheet.Shapes("Graphique 19").Name = "PMDVID"
With ActiveSheet.Shapes("PMDVID")
.Left = Range("B370").Left
.Top = Range("B370").Top
End With​
Du coup, si je n'exécute pas les macros dans l'ordre, ça ne marche pas.
Ma question c'est : est-il possible de définir ce nom en amont au moment de l'ajout dans la page afin que le nom de base soit toujours le même ? (j'ai fait une chose comme ça pour ajouter des nouvelles feuilles dans mon classeur avec toujours le même nom mais je n'ai pas réussi à le reproduire pour les graphs...)
Ou bien de désactiver l'incrémentation ???

Merci de votre aide

Ludoxa
 

mromain

XLDnaute Barbatruc
Re : VBA définir le nom du graphique lors de l'ajout

bonjour ludoxa,

si tu n'a qu'un seul graphique dont le nom commence par "Graphique", tu peux essayer de le renommer avec cette macro
Code:
For Each laShape In ActiveSheet.Shapes
    If laShape.Name Like "Graphique*" Then laShape.Name = "PMDVID"
Next laShape

sinon, il faudrait modifier tes macro de "création de graphique" pour le renommer dès leur création.

a+
 

ludoxa

XLDnaute Nouveau
Re : VBA définir le nom du graphique lors de l'ajout

bonjour ludoxa,

si tu n'a qu'un seul graphique dont le nom commence par "Graphique", tu peux essayer de le renommer avec cette macro
Code:
For Each laShape In ActiveSheet.Shapes
    If laShape.Name Like "Graphique*" Then laShape.Name = "PMDVID"
Next laShape

sinon, il faudrait modifier tes macro de "création de graphique" pour le renommer dès leur création.

a+

tu définis laShape as string ?
 

ludoxa

XLDnaute Nouveau
Re : VBA définir le nom du graphique lors de l'ajout

Voici mon code en entier, merci d'avance.

Dim feuille As Worksheet
Dim plage As Range
' DECLARATION tableau avec tous les noms des feuilles
Set feuilles = Sheets(Array("AIGUEPERSE", "ALBIGNY-SUR-SAONE", "ALIX", "AMPLEPUIS", "AMPUIS", "ANSE", "ARNAS", "AVEIZE", "AVENAS", "BAGNOLS", "BEAUJEU", "BELLEVILLE", "BESSENAY", "BLACÉ", "BOURG DE THIZY", "BRIGNAIS", "BRINDAS", "BRULLIOLES", "BRUSSIEU", "BULLY", "CAILLOUX-SUR-FONTAINES", "CHAMBOST-LONGESSAIGNE", "CHAMELET", "CHAMPAGNE-AU-MONT-D'OR", "CHAPONNAY", "CHAPONOST", "CHARBONNIERES-LES-BAINS", "CHARENTAY", "CHARLY", "CHARNAY", "CHASSAGNY", "CHASSELAY", "CHASSIEU", "CHATILLON D'AZERGUES - CHESSY", "CHAUSSAN", "CHAZAY D'AZERGUES", "CHEVINAY", "CHIROUBLES", "CLAVEISOLLES", "COGNY", "COISE", "COLLONGES-AU-MONT-D'OR", "COLOMBIER-SAUGNIEU", "COMMUNAY", "CONDRIEU", "CORBAS", "CORCELLES-EN-BEAUJOLAIS", "COURS-LA-VILLE", "COURZIEU", "COUZON-AU-MONT-D'OR", "CRAPONNE", "CUBLIZE", "CURIS-AU-MONT-D'OR", "DARDILLY", "DENICÉ", "DOMMARTIN", "DUERNE", "ECHALAS", "EVEUX", "FEYZIN", "FLEURIE", "FLEURIEU-SUR-SAONE", "FLEURIEUX-SUR-L'ARBRESLE", "FONTAINES-SUR-SAONE", "FRANCHEVILLE"))

For Each feuille In feuilles
' DECLARATION plage des données source
Set plage = feuille.Range("AO1:AO8")
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=plage, PlotBy _
:=xlColumns
ActiveChart.SeriesCollection(1).XValues = feuille.Range("A2:A8")
ActiveChart.SeriesCollection(1).Name = feuille.Range("AO1")
' MISE EN FORME localisation du graphique dans la feuille
ActiveChart.Location Where:=xlLocationAsObject, Name:=feuille.Name
' MISE EN FORME titre légende
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Evolution des prêts MD Vidéo"
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
.HasLegend = False
End With
ActiveChart.HasLegend = False
ActiveChart.ApplyDataLabels Type:=xlDataLabelsShowValue, LegendKey:=False
' MISE EN FORME changement de couleur
ActiveChart.SeriesCollection(1).Points(1).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
With Selection.Interior
' vert relais
.ColorIndex = 43
.Pattern = xlSolid
End With
ActiveChart.SeriesCollection(1).Points(2).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
With Selection.Interior
' vert relais
.ColorIndex = 43
.Pattern = xlSolid
End With
ActiveChart.SeriesCollection(1).Points(3).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
With Selection.Interior
' vert relais
.ColorIndex = 43
.Pattern = xlSolid
End With
ActiveChart.SeriesCollection(1).Points(4).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
With Selection.Interior
' vert relais
.ColorIndex = 43
.Pattern = xlSolid
End With
ActiveChart.SeriesCollection(1).Points(5).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
With Selection.Interior
' vert relais
.ColorIndex = 43
.Pattern = xlSolid
End With
ActiveChart.SeriesCollection(1).Points(6).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
With Selection.Interior
' bleu réseau
.ColorIndex = 41
.Pattern = xlSolid
End With
ActiveChart.SeriesCollection(1).Points(7).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
With Selection.Interior
' jaune md
.ColorIndex = 44
.Pattern = xlSolid
End With
' MISE EN FORME renommer le graphique pour pouvoir le placer
ActiveSheet.Shapes("Graphique 19").Name = "PMDVID"
With ActiveSheet.Shapes("PMDVID")
.Left = Range("B370").Left
.Top = Range("B370").Top
End With
' FORMATAGE ne pas déplacer et dimensionner avec les cellules
With ActiveSheet.DrawingObjects("PMDVID")
.Placement = xlFreeFloating
.PrintObject = True
End With
ActiveSheet.DrawingObjects("PMDVID").Locked = True

Next

End Sub
 

mromain

XLDnaute Barbatruc
Re : VBA définir le nom du graphique lors de l'ajout

re,

essaye de le mettre ici :
Code:
Dim feuille As Worksheet
Dim plage As Range
' DECLARATION tableau avec tous les noms des feuilles
Set feuilles = Sheets(Array("AIGUEPERSE", "ALBIGNY-SUR-SAONE", "ALIX", "AMPLEPUIS", "AMPUIS", "ANSE", "ARNAS", "AVEIZE", "AVENAS", "BAGNOLS", "BEAUJEU", "BELLEVILLE", "BESSENAY", "BLACÉ", "BOURG DE THIZY", "BRIGNAIS", "BRINDAS", "BRULLIOLES", "BRUSSIEU", "BULLY", "CAILLOUX-SUR-FONTAINES", "CHAMBOST-LONGESSAIGNE", "CHAMELET", "CHAMPAGNE-AU-MONT-D'OR", "CHAPONNAY", "CHAPONOST", "CHARBONNIERES-LES-BAINS", "CHARENTAY", "CHARLY", "CHARNAY", "CHASSAGNY", "CHASSELAY", "CHASSIEU", "CHATILLON D'AZERGUES - CHESSY", "CHAUSSAN", "CHAZAY D'AZERGUES", "CHEVINAY", "CHIROUBLES", "CLAVEISOLLES", "COGNY", "COISE", "COLLONGES-AU-MONT-D'OR", "COLOMBIER-SAUGNIEU", "COMMUNAY", "CONDRIEU", "CORBAS", "CORCELLES-EN-BEAUJOLAIS", "COURS-LA-VILLE", "COURZIEU", "COUZON-AU-MONT-D'OR", "CRAPONNE", "CUBLIZE", "CURIS-AU-MONT-D'OR", "DARDILLY", "DENICÉ", "DOMMARTIN", "DUERNE", "ECHALAS", "EVEUX", "FEYZIN", "FLEURIE", "FLEURIEU-SUR-SAONE", "FLEURIEUX-SUR-L'ARBRESLE", "FONTAINES-SUR-SAONE", "FRANCHEVILLE"))

For Each feuille In feuilles
    ' DECLARATION plage des données source
    Set plage = feuille.Range("AO1:AO8")
    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=plage, PlotBy _
    :=xlColumns
    ActiveChart.SeriesCollection(1).XValues = feuille.Range("A2:A8")
    ActiveChart.SeriesCollection(1).Name = feuille.Range("AO1")
    ' MISE EN FORME localisation du graphique dans la feuille
    ActiveChart.Location Where:=xlLocationAsObject, Name:=feuille.Name
    ' MISE EN FORME titre légende
    With ActiveChart
        [COLOR=Red][B].Name = "leNom"[/B][/COLOR]
        .HasTitle = True
        .ChartTitle.Characters.Text = "Evolution des prêts MD Vidéo"
        .Axes(xlCategory, xlPrimary).HasTitle = False
        .Axes(xlValue, xlPrimary).HasTitle = False
        .HasLegend = False
    End With
    ActiveChart.HasLegend = False
    ActiveChart.ApplyDataLabels Type:=xlDataLabelsShowValue, LegendKey:=False
    ' MISE EN FORME changement de couleur
    ActiveChart.SeriesCollection(1).Points(1).Select
    With Selection.Border
        .Weight = xlThin
        .LineStyle = xlAutomatic
    End With
    Selection.Shadow = False
    Selection.InvertIfNegative = False
    With Selection.Interior
        ' vert relais
        .ColorIndex = 43
        .Pattern = xlSolid
    End With
    ActiveChart.SeriesCollection(1).Points(2).Select
    With Selection.Border
        .Weight = xlThin
        .LineStyle = xlAutomatic
    End With
    Selection.Shadow = False
    Selection.InvertIfNegative = False
    With Selection.Interior
        ' vert relais
        .ColorIndex = 43
        .Pattern = xlSolid
    End With
    ActiveChart.SeriesCollection(1).Points(3).Select
    With Selection.Border
        .Weight = xlThin
        .LineStyle = xlAutomatic
    End With
    Selection.Shadow = False
    Selection.InvertIfNegative = False
    With Selection.Interior
        ' vert relais
        .ColorIndex = 43
        .Pattern = xlSolid
    End With
    ActiveChart.SeriesCollection(1).Points(4).Select
    With Selection.Border
        .Weight = xlThin
        .LineStyle = xlAutomatic
    End With
    Selection.Shadow = False
    Selection.InvertIfNegative = False
    With Selection.Interior
        ' vert relais
        .ColorIndex = 43
        .Pattern = xlSolid
    End With
    ActiveChart.SeriesCollection(1).Points(5).Select
    With Selection.Border
        .Weight = xlThin
        .LineStyle = xlAutomatic
    End With
    Selection.Shadow = False
    Selection.InvertIfNegative = False
    With Selection.Interior
        ' vert relais
        .ColorIndex = 43
        .Pattern = xlSolid
    End With
    ActiveChart.SeriesCollection(1).Points(6).Select
    With Selection.Border
        .Weight = xlThin
        .LineStyle = xlAutomatic
    End With
    Selection.Shadow = False
    Selection.InvertIfNegative = False
    With Selection.Interior
        ' bleu réseau
        .ColorIndex = 41
        .Pattern = xlSolid
    End With
    ActiveChart.SeriesCollection(1).Points(7).Select
    With Selection.Border
        .Weight = xlThin
        .LineStyle = xlAutomatic
    End With
    Selection.Shadow = False
    Selection.InvertIfNegative = False
    With Selection.Interior
        ' jaune md
        .ColorIndex = 44
        .Pattern = xlSolid
    End With
    ' MISE EN FORME renommer le graphique pour pouvoir le placer
    ActiveSheet.Shapes("Graphique 19").Name = "PMDVID"
    With ActiveSheet.Shapes("PMDVID")
        .Left = Range("B370").Left
        .Top = Range("B370").Top
    End With
    ' FORMATAGE ne pas déplacer et dimensionner avec les cellules
    With ActiveSheet.DrawingObjects("PMDVID")
        .Placement = xlFreeFloating
        .PrintObject = True
    End With
    ActiveSheet.DrawingObjects("PMDVID").Locked = True

Next

a+
 

ludoxa

XLDnaute Nouveau
Re : VBA définir le nom du graphique lors de l'ajout

Ca ne marche pas, il me dit :
Erreur d'exécution 1004
La méthode Name de l'objet _Chart a échoué

J'ai aussi essayé avec lashape as shape, là ça ne plante pas mais ça ne fait rien...

Tu as une autre idée ?
 

Pierrot93

XLDnaute Barbatruc
Re : VBA définir le nom du graphique lors de l'ajout

Re Ludoxa, Romain

un exemple ci dessous de création de graphique incorporé par vba, sans utilisation de nom mais en utilisant une variable "ChartObject", si cela peut te servir... :

Code:
Option Explicit
Sub CreationGraphique()
Dim c As ChartObject
Set c = Feuil1.ChartObjects.Add(Range("G15").Left, Range("G15").Top, 300, 200)
With c.Chart
    .ChartType = xlLineMarkers
    .SeriesCollection.Add Range("C1:C17"), , True
    .SeriesCollection(1).XValues = Range("A2:A17")
    With .Legend
        .Position = xlBottom
        With .Font
            .Size = 8
            .Bold = True
        End With
    End With
    With .ChartTitle
        .Text = "mongraph"
        With .Font
            .Size = 8
            .Bold = True
        End With
    End With
    With .Axes(xlCategory).TickLabels
        .Font.Size = 8
        .NumberFormat = "dd/mm/yy"
        .Orientation = 45
    End With
    .Axes(xlValue).TickLabels.Font.Size = 8
End With
End Sub

bonne fin d'après midi
@+
 

Discussions similaires

Statistiques des forums

Discussions
312 196
Messages
2 086 101
Membres
103 116
dernier inscrit
kutobi87