IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Générer un graphique sous Excel en VBA


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 9
    Par défaut Générer un graphique sous Excel en VBA
    Bonjour,
    Dans une feuille excel, j'ai 3 colonnes de données et j'aimerai obtenir un graphique.
    En utilisant l'enregistreur de macro excel j'obtiens ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    Sub Macro3()
        Range("B1:D18").Select
        ActiveSheet.Shapes.AddChart.Select
        ActiveChart.SetSourceData Source:=Range("'Résultats'!$B$1:$D$18")
        ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
    End Sub
    Quand je lance la macro, j'ai un message d'erreur :
    Erreur d'exécution 1004
    Dimension spécifiée non valide pour le type de graphique en cours.

    ça fait qq heures que je parcours les forums en quête de trouver un moyen de tracer un graphique, et aucune solutions fonctionnent.
    (NB : je n'ai jamais programmé en VBA)
    Est ce que qq'un aurait une réponse ?

  2. #2
    Invité
    Invité(e)
    Par défaut
    Bonjour;
    regarde ce lien: http://www.developpez.net/forums/d14...e/#post7650128

    ouvre mon fichier, ouvre ton fichier maintiens le click gauche de la sourie et fait glisser le module de classe ClsGraph de mon projet vers ton Projet puis adapte la sub test à ton Projet.

    le module de classe ClsGraph est une boîte à outils que tu utilise pour générer n'importe quel graph, tu n'as pas à y toucher!

    Code Module1 : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Sub test()
     
    Dim Plage As Range
    Dim Graph As New ClsGraph
    Set Plage = Range("A1:C12")
    Graph.Delete Sheets("Graph1"), "Graph1"
    Graph.Nouveau Sheets("Graph1"), "Graph1"
    Graph.Style ExlConeCol
    Graph.Source Plage, xlRows
    Graph.SeriesCollection "=Recap!$C$64:$CP$64"
    Graph.SeriesCollection_Caption "Mois", msoElementPrimaryCategoryAxisTitleAdjacentToAxis
    Graph.SeriesCollection_Caption "ETPs", msoElementPrimaryValueAxisTitleRotated
    Graph.SeriesCollection_Caption "Plan de charge Technique groupé par famille", msoElementChartTitleAboveChart
    Graph.Position 0, 0
    Graph.Taille 550, 700
    Graph.SeriesCollection_Visible
    Graph.SaveAs_Image Sheets("Graph1"), "Graph1", ActiveWorkbook.Path & "\" & "graphe.gif"
     
    End Sub
    Code Classe ClsGraph : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    Dim Shap As Shape
    Enum Style
        ExlColumnClustered = xlColumnClustered
        ExlColumnStacked = xlColumnStacked
        ExlColumnStacked100 = xlColumnStacked100
        Exl3DColumnClustered = xl3DColumnClustered
        Exl3DColumnStacked = xl3DColumnStacked
        Exl3DColumnStacked100 = xl3DColumnStacked100
        Exl3DColumn = xl3DColumn
        ExlCylinderColClustered = xlCylinderColClustered
        ExlCylinderColStacked = xlCylinderColStacked
        ExlCylinderColStacked100 = xlCylinderColStacked100
        ExlCylinderCol = xlCylinderCol
        ExlConeColClustered = xlConeColClustered
        ExlConeColStacked = xlConeColStacked
        ExlConeColStacked100 = xlConeColStacked100
        ExlConeCol = xlConeCol
        ExlPyramidColClustered = xlPyramidColClustered
        ExlPyramidColStacked = xlPyramidColStacked
        ExlPyramidColStacked100 = xlPyramidColStacked100
        ExlPyramidCol = xlPyramidCol
        ExlLine = xlLine
        ExlLineStacked = xlLineStacked
        ExlLineStacked100 = xlLineStacked100
        ExlLineMarkers = xlLineMarkers
        ExlLineMarkersStacked = xlLineMarkersStacked
        ExlLineMarkersStacked100 = xlLineMarkersStacked100
        Exl3DLine = xl3DLine
        ExlPie = xlPie
        ExlPieExploded = xlPieExploded
        ExlPieOfPie = xlPieOfPie
        ExlBarOfPie = xlBarOfPie
        Exl3DPie = xl3DPie
        Exl3DPieExplodede = xl3DPieExploded
        ExlBarClustered = xlBarClustered
        ExlBarStacked = xlBarStacked
        ExlBarStacked100 = xlBarStacked100
        Exl3DBarClustered = xl3DBarClustered
        Exl3DBarStacked = xl3DBarStacked
        Exl3DBarStacked100 = xl3DBarStacked100
        ExlCylinderBarClustered = xlCylinderBarClustered
        ExlCylinderBarStacked = xlCylinderBarStacked
        ExlCylinderBarStacked100 = xlCylinderBarStacked100
        ExlConeBarClustered = xlConeBarClustered
        ExlConeBarStacked = xlConeBarStacked
        ExlConeBarStacked100 = xlConeBarStacked100
        ExlPyramidBarClustered = xlPyramidBarClustered
        ExlPyramidBarStacked = xlPyramidBarStacked
        ExlPyramidBarStacked100 = xlPyramidBarStacked100
        ExlArea = xlArea
        ExlAreaStacked = xlAreaStacked
        ExlAreaStacked100 = xlAreaStacked100
        Exl3DArea = xl3DArea
        Exl3DAreaStacked = xl3DAreaStacked
        Exl3DAreaStacked100 = xl3DAreaStacked100
        ExlXYScatter = xlXYScatter
        ExlXYScatterSmooth = xlXYScatterSmooth
        ExlXYScatterSmoothNoMarkers = xlXYScatterSmoothNoMarkers
        ExlXYScatterLines = xlXYScatterLines
        ExlXYScatterLinesNoMarkers = xlXYScatterLinesNoMarkers
        ExlDoughnut = xlDoughnut
        ExlDoughnutExploded = xlDoughnutExploded
        ExlBubble = xlBubble
        ExlBubble3DEffect = xlBubble3DEffect
        ExlRadar = xlRadar
        ExlRadarMarkers = xlRadarMarkers
        ExlRadarFilled = xlRadarFilled
        ExlSurface = xlSurface
        ExlSurfaceWireframe = xlSurfaceWireframe
        ExlSurfaceTopView = xlSurfaceTopView
        ExlSurfaceTopViewWireframe = xlSurfaceTopViewWireframe
    End Enum
     
    Public Sub SaveAs_Image(Feuille As Worksheet, Non As String, fichier As String)
    For Each MyObject In Feuille.Shapes
        If MyObject.Name = Non Then
            MyObject.Chart.Export Filename:=fichier, FilterName:="GIF"
            Exit Sub
        End If
    Next
    End Sub
    Public Sub Delete(Feuille As Worksheet, Non As String)
    For Each MyObject In Feuille.Shapes
        If MyObject.Name = Non Then MyObject.Delete: Exit Sub
    Next
    End Sub
    Public Sub Nouveau(Feuille As Worksheet, Non As String)
    Set Shap = Feuille.Shapes.AddChart
    Shap.Name = Non
    End Sub
    Public Sub Source(MyRange As Range, Orientation As Long)
     Shap.Chart.SetSourceData MyRange, Orientation
    End Sub
    Public Sub Style(MyStyle As Style)
     Shap.Chart.ChartType = MyStyle
    End Sub
    Public Sub SeriesCollection_Caption(Caption As String, Element As MsoChartElementType)
    Dim e
      Shap.Chart.SetElement Element
      Select Case Element
                Case 2
                    Shap.Chart.ChartTitle.Text = Caption
                Case 301
                    Shap.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Text = Caption
                Case 309
                    Shap.Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = Caption
      End Select
    End Sub
    Public Sub SeriesCollection(V)
     Shap.Chart.SeriesCollection(1).XValues = V
    End Sub
    Public Sub SeriesCollection_Visible()
     
     Shap.Chart.SeriesCollection(1).ApplyDataLabels
    End Sub
    Public Sub Taille(Hauteur As Integer, Largeur As Integer)
     Shap.Height = Hauteur
     Shap.Width = Largeur
    End Sub
    Public Sub Position(X As Integer, Y As Integer)
     Shap.Top = X
     Shap.Left = Y
    End Sub

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 9
    Par défaut
    Mais pourquoi la commande ne fonctionne pas ?
    D'autre part j'ai essayé le code, mais il envoie un message d'erreur : erreur d'exécution 91. variable objet ou variable de bloc with non définie...

  4. #4
    Invité
    Invité(e)
    Par défaut
    tu as ouvert le fichier en suivant le lien que je t'es donné et les instruction pour intégrer le module de classe dans ton projet?

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 9
    Par défaut
    Je reviens car je n'ai toujours pas trouvé la solution.
    @ rdurupt : j'ai suivi les instructions mais j'ai toujours ce fameux message d'erreur.

    J'ai essayé ce code trouvé sur ce site :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    Sub Graphe()
    Dim i As Integer
    Dim MaPlage As Range
        Range("A1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        NbLigne = Sheets("Feuil1").UsedRange.Rows.Count
        NbCol = Sheets("Feuil1").UsedRange.Columns.Count
        Sheets("Feuil1").Shapes.AddChart.Select
        ActiveChart.SetSourceData Source:=Worksheets("Feuil1").Range(Cells(1, 1), Cells(NbLigne, NbCol))
        ActiveChart.ChartType = xlLine
        ActiveChart.SetElement (msoElementChartTitleAboveChart)
     
            With ActiveChart
                .HasTitle = True
                .ChartTitle.Characters.Text = "Nbre de cotations"
            End With
    End Sub
    Ce code est censé fonctionner, mais chez moi toujours le code d'erreur 1004 sur cette partie : Sheets("Feuil1").Shapes.AddChart.Select
    Personne ne sait ?

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2010
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2010
    Messages : 9
    Par défaut
    En essayant tous, j'ai trouvé finalement la solution :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ActiveSheet.Shapes.AddChart (xlXYScatterSmoothNoMarkers)
    Le type de graphique est optionnel mais obligatoire finalement !!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 24/10/2010, 09h18
  2. [VB.NET] Création de graphique sous excel
    Par krfa1 dans le forum Windows Forms
    Réponses: 2
    Dernier message: 02/06/2006, 10h11
  3. Réaliser un graphique sous Excel
    Par vdbadr dans le forum Contribuez
    Réponses: 1
    Dernier message: 30/05/2006, 15h37
  4. [VBA-E]Deplacer un graphique sous Excel
    Par gaudy dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 02/04/2006, 06h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo