3

I have a macro to create a chart and modify its properties.

If I run the macro normally, I get

Run-time error '-2147024809 (80070057)':
The Item with the specified name wasn't found.

enter image description here

on the last line of this code

Set ChtObj = Worksheets("summary").ChartObjects.Add(Left:=700, Top:=100, Width:=800, Height:=500)
With ChtObj
    .chart.ChartType = xlColumnStacked
    .chart.SetSourceData Source:=Range("statistics!A101:C" & targetRow)
    .Name = "waterfall"
End With
ActiveSheet.ChartObjects("waterfall").Activate

If I run the code step by step, I get no errors.

Community
  • 1
  • 1
L.Dutch
  • 926
  • 3
  • 17
  • 38

1 Answers1

3

Write Worksheets("summary").Activate on the top of your code, it should work.

The problem is that during the macro running, your active sheet is not "summary". In general, using Activesheet is not a good practice in VBA - How to avoid using Select in Excel VBA.

Vityata
  • 42,633
  • 8
  • 55
  • 100