0

Why does this line below doesn't work?

ActiveSheet.Shapes.AddChart2(204, xlColumnClustered).Select

but this one do

graphe_clos.Cells(2, 1).Select
ActiveSheet.Shapes.AddChart2(204, xlColumnClustered).Select

And i intend to create multiple chart and pivot table on the same sheet so do i need to always specify a cell?

Ibrahim
  • 79
  • 10
  • Why use `select` anyhow? Do a `With ActiveSheet.Shapes.AddChart2(204, xlColumnClustered)` or assign the result to a variable. – FunThomas Aug 23 '18 at 07:31
  • i use select because i have to do this `With ActiveChart .ClearToMatchStyle .ChartStyle = 257 .HasTitle = True .ChartTitle.Text = " Nombre d'évènement" .ShowValueFieldButtons = False .ShowLegendFieldButtons = False End With` – Ibrahim Aug 23 '18 at 07:32
  • No, it's not necessary to do this on `ActiveChart`. – FunThomas Aug 23 '18 at 07:48
  • and how should i do it? – Ibrahim Aug 23 '18 at 08:07
  • if i do `With ActiveSheet.Shapes.AddChart2(204, xlColumnClustered)` it's asking me to assign it to a variable, it's better to create a variable for it then using `.select`? – Ibrahim Aug 23 '18 at 08:09

2 Answers2

1

Use .AddChart instead of .AddChart2. That works for me. Then later you can edit the chart properties in a With block.

iSpain17
  • 2,502
  • 3
  • 17
  • 26
1

This should work (had to add .Chart in the with statement)

with ActiveSheet.Shapes.AddChart2(204, xlColumnClustered).Chart
    .ClearToMatchStyle 
    .ChartStyle = 257 
    .HasTitle = True 
    .ChartTitle.Text = " Nombre d'évènement" 
end with

However, the properties .ShowValueFieldButtons and .ShowValueFieldButtons are not valid (I think they are valid only for PivotCharts). It gives error messages no matter if or if not using ActiveChart.

As a general advice: Read How to avoid using Select in Excel VBA

FunThomas
  • 23,043
  • 3
  • 18
  • 34