VBA problème création graphique

Kouik Kouik jr

XLDnaute Occasionnel
Bonsoir tout le monde !

j'ai de nouveau un petit problème que je n'arrive pas a resoudre.

J'essaie de créer 3 graphiques dans la feuille "Chart" mais cela bloque sur la ligne de code

"ActiveSheet.Shapes.AddChart.Select" et je ne comprend pas pourquoi ?

Quelqu'un aurait il une idée ?

Mille merci d'avance:)

HTML:
Sub Macro2()


Application.ScreenUpdating = False
Application.DisplayAlerts = False


    With Sheets("Feuil2")
    
    Range("A:A,H:H").Select
    Range("H1").Activate
    ActiveSheet.Shapes.AddChart.Select
    
    ActiveChart.SetSourceData Source:=Range("Feuil2!$A:$A;Feuil2!$H:$H")
    ActiveChart.ChartType = xl3DLine
    
    ActiveChart.Axes(xlCategory, xlSecondary).Select
    Selection.Delete
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Chart"
    ActiveSheet.ChartObjects("Graphique 4").Activate
    Sheets("Feuil2").Select
    Range("J:J,M:M").Select
    Range("M1").Activate
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Range("Feuil2!$J:$J;Feuil2!$M:$M")
    ActiveChart.ChartType = xl3DLine
    ActiveChart.Axes(xlCategory, xlSecondary).Select
    Selection.Delete
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Chart"
    ActiveSheet.ChartObjects("Graphique 5").Activate
    ActiveChart.ChartTitle.Select
    Selection.Left = 154.147
    Selection.Top = 0
    ActiveSheet.ChartObjects("Graphique 5").Activate
    ActiveChart.ChartArea.Select
    ActiveSheet.ChartObjects("Graphique 5").Activate
    ActiveChart.ChartTitle.Select
    Selection.Left = 66.147
    Selection.Top = 15
    Sheets("Feuil2").Select
    Columns("J:J").Select
    ActiveWindow.SmallScroll ToRight:=3
    Range("J:J,N:N").Select
    Range("N1").Activate
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Range("Feuil2!$J:$J;Feuil2!$N:$N")
    ActiveChart.ChartType = xl3DLine
    ActiveChart.Axes(xlCategory, xlSecondary).Select
    Selection.Delete
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Chart"
    ActiveSheet.ChartObjects("Graphique 6").Activate
   End With
   
   Application.ScreenUpdating = True
Application.DisplayAlerts = True

    
End Sub
 

Kouik Kouik jr

XLDnaute Occasionnel
Re : VBA problème création graphique

Apparament le problème vient de là !

ActiveChart.SetSourceData Source:=Range("Feuil2!$A$A:$H$H")

Je vois pas ce qui cloche !:mad:

HTML:
Sub cv()

Range("B3:B8").Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.SetSourceData Source:=Range("Feuil2!$A$A:$H$H")
    ActiveChart.ChartType = xl3DColumn
End Sub
 

Hulk

XLDnaute Barbatruc
Re : VBA problème création graphique

Hello,

Ai regardé ta macro, plusieurs erreurs que j'ai corrigé avec l'enregistreur.

Tu devras sûrement le modifier à ta guise, mais c'est surtout pour que tu te rendes compte avec quoi tu peux utiliser le Bloc With ou pas.
Code:
Sub Macro2()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Sheets("Feuil2").Range("A:A,H:H"), PlotBy _
        :=xlColumns
    ActiveChart.ChartType = xl3DLine
'    ActiveChart.Location Where:=xlLocationAsNewSheet 'Si tu veux le graphique sur une feuille Graphique
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Feuil2" '* "Feuil2" = 6 caractères. Sur la feuille
    
    With ActiveChart 'Pour le nom
        Graph1 = Right(.Name, Len(.Name) - 6) '* 6 caractères
    End With
        
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "Graphique N° 1"
        .Axes(xlCategory, xlPrimary).HasTitle = False
        .Axes(xlValue, xlPrimary).HasTitle = False
    End With
    
    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Sheets("Feuil2").Range("J:J,M:M"), PlotBy _
        :=xlColumns
    ActiveChart.ChartType = xl3DLine
'    ActiveChart.Location Where:=xlLocationAsNewSheet
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Feuil2"
    
    With ActiveChart 'Pour le nom
        Graph2 = Right(.Name, Len(.Name) - 6)
    End With
    
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "Graphique N° 2"
        .Axes(xlCategory, xlPrimary).HasTitle = False
        .Axes(xlValue, xlPrimary).HasTitle = False
    End With

    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SetSourceData Source:=Sheets("Feuil2").Range("J:J,N:N"), PlotBy _
        :=xlColumns
    ActiveChart.ChartType = xl3DLine
'    ActiveChart.Location Where:=xlLocationAsNewSheet
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Feuil2"
    
    With ActiveChart
        Graph3 = Right(.Name, Len(.Name) - 6)
    End With

    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "Graphique N° 3"
        .Axes(xlCategory, xlPrimary).HasTitle = False
        .Axes(xlValue, xlPrimary).HasTitle = False
    End With

'Pour ce qui suit pas besoin de With ************************

'Graphique 1
    ActiveSheet.ChartObjects(Graph1).Activate
    ActiveSheet.Shapes(Graph1).IncrementLeft -223.5
    ActiveSheet.Shapes(Graph1).IncrementTop -104.25
    ActiveChart.ChartTitle.Select
    Selection.Left = 154.147
    Selection.Top = 12

'Graphique 2
    ActiveSheet.ChartObjects(Graph2).Activate
    ActiveSheet.Shapes(Graph2).IncrementLeft -223.5
    ActiveSheet.Shapes(Graph2).IncrementTop 123#
    ActiveChart.ChartTitle.Select
    Selection.Left = 66.75
    Selection.Top = 15

'Graphique 3
    ActiveSheet.ChartObjects(Graph3).Activate
    ActiveSheet.Shapes(Graph3).IncrementLeft 165#
    ActiveSheet.Shapes(Graph3).IncrementTop -27#
    ActiveChart.ChartTitle.Select
    Selection.Left = 120
    Selection.Top = 12

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
A toi de jouer !

Cdt, Hulk.
 

Kouik Kouik jr

XLDnaute Occasionnel
Re : VBA problème création graphique

Merci Hulk de ton aide ,

C'est vrai que je n'avais pas répondu a ton aide mais je pensais que personne ne s'était finalement penché sur mon problème et j'ai reussi à le resoudre autrement.

Merci beaucoup

Voici le code que j'utilise et qui marche (pour le moment ;) )

Je vais regarder ton code pour voir si je peux faire des améliorations sur le mien

HTML:
Sub Crea()

'Creation graphique

    Dim HAUT As Integer, maplage As Range

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    'Creation graphique
    For i = 1 To 4
        With Sheets("Feuil2")
            If i = 1 Then HAUT = 45: Set maplage = .Range("A:A,H:H")
            If i = 2 Then HAUT = 300: Set maplage = .Range("A:A,M:M")
            If i = 3 Then HAUT = 650: Set maplage = .Range("A:A,N:N")
            If i = 4 Then HAUT = 900: Set maplage = .Range("A:A,M:M,O:O")
        End With
        With ActiveSheet.ChartObjects.Add(Left:=100, Width:=575, Top:=HAUT, Height:=225)
            .Chart.SetSourceData Source:=maplage
            .Chart.ChartType = xlXYScatterLines
            .Chart.Legend.Select
            Selection.Delete
            ActiveChart.Location Where:=xlLocationAsObject, Name:="Chart"
        End With
    Next

End Sub
 

Discussions similaires

Statistiques des forums

Discussions
312 765
Messages
2 091 893
Membres
105 086
dernier inscrit
hyacinthe