0

I have an embedded chart in the sheet "Daten_G9-G10". In the same sheet there's the name "Chart9", which is a dynamic range:

=OFFSET('Daten_G9-G10'!$G$31;0;0;MAX('Daten_G9-G10'!$F$31:$F$51)+1;COUNTA('Daten_G9-G10'!$G$30:$AAA$30))

My goal is to update the chart's data range.

Sub UpdateChartSourceData()
  With ActiveSheet
   .ChartObjects(1).Chart.SetSourceData _
   Source:=.Range("Chart9"), _
   PlotBy:=xlRows
  End With
End Sub

This macro worked well so far. But I've moved the chart into a new sheet: "G9". That's where my problem begins: "Runtime error '438', Object doesn't support this property or method".

How do I have to adjust the macro that I can update this chart, still referring to the name in the sheet "Daten_G9-G10" as the chart's data range?

Community
  • 1
  • 1
thoscha
  • 35
  • 5
  • You ran the macro on the sheet with the chart right? – z̫͋ May 13 '14 at 12:07
  • First yes. But now the former (embedded) chart is a chart sheet itself. I would like to run the macro on any sheet, if its possible. – thoscha May 13 '14 at 12:42

1 Answers1

0

RIght now, your code only refers to ActiveSheet, which is causing an error probably because your chart is on the ActiveSheet, but the data is on another sheet.

Try this:

Sub UpdateChartSourceData()
  ' ## Modify the next line to refer to your chart sheet's name:
  With Worksheets("_Sheet_With_Chart_")
      .ChartObjects(1).Chart.SetSourceData _
          Source:=Worksheets("Daten_G9-G10").Range("Chart9"), _
          PlotBy:=xlRows
  End With
End Sub

Update from comments

If dealing with a Chart Sheet, modify to use the Sheets collection (Worksheets will raise an error)

Sub UpdateChartSourceData()
  ' ## Modify the next line to refer to your chart sheet's name:
  With Sheets("_Sheet_With_Chart_")
          .Chart.SetSourceData Source:=Worksheets("Daten_G9-G10").Range("Chart9"), _
          PlotBy:=xlRows
  End With
End Sub
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • 1
    okay I've changed the code. unfortunately I got a new error: "The specified dimension is not valid for the current chart type." Is it because "G9" is a chart sheet? thanks in advance – thoscha May 13 '14 at 12:35
  • 1
    For a chart Sheet you don't need `.ChartObjects(1).Chart` just use `.SetSourceData` – z̫͋ May 13 '14 at 12:51
  • @thoscha you'll also have to refer to `Sheets(_chartsheet_name)` instead of `Worksheets`. I've updated my answer above :) – David Zemens May 13 '14 at 13:45