Assuming "chart_sheet"
is the name of your Chart
and "data_sheet"
is the name of your Worksheet
, I think you want to do the following:
Charts("chart_sheet").SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
Your With
block was not doing anything useful - the purpose of a With
block is to allow you to just type .
as a shortcut for something like Worksheets("data_sheet").
.
So something like:
With Sheets("chart_sheet")
.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")
End With
would work, because the .SetSourceData
is an abbreviation of Sheets("chart_sheet").SetSourceData
. (Notice also that the Sheets
collection contains both Worksheets
and Charts
objects, so Charts("chart_sheet")
and Sheets("chart_sheet")
both point to the same thing.)
ActiveChart
refers to the currently active chart, just as ActiveSheet
returns to the currently sheet. If you don't have a chart active when that piece of code executes, you will get an error.
So the following piece of code would also probably have worked for you:
Sheets("chart_sheet").Activate
ActiveChart.SetSourceData Source:=Worksheets("data_sheet").Range("A1:A20")