With ActiveWorkbook.Sheets(1)
INSPECT_CHARTS_NUMBER = .ChartObjects.Count
For c= 1 To .ChartObjects.Count
Set INSPECT_CHART = .ChartObjects(c).Duplicate
.ChartObjects(c).Chart.SetSourceData Source:=.Range("e4:h4")
Next lngC
End With
I do the above to change the source data of a chart in VBA this line .Chart.SetSourceData Source:=.Range("e4:h4")
does do job, but this will not work if there are multiple series in that chart.
How can I get the sourcedata of all the series and then how can I change them separately?
Here is an example
Let's say the series collection of the active chart is picking the values from
A1:A5
andC1:C5
. So the below code will change the source toA8:A12
andC8:C12
Screenshot
Before
After
You can access the data source of the chart series by retrieving the series .Formula string.
After that you can change the Formula (with Replace for example) and then reapply the Formula for the series.
Code example (provided for the solution from Sid):
Be sure to use the $ sign, because the series .Formula uses numbers for the chart Type representation. The sign helps to omit replacing the chart Type in the formula.
Hope this helps...
Use
.seriescollection
More on this link.