Excel/VBA: Chart.SetSourceData is changing the range by itself

baldmosher

New Member
Joined
Jul 10, 2009
Messages
32
Excel 2010

I'm trying to update a chart source data range with VBA but it's pulling in the wrong range.

where the data source is "graph_01" because it's January data (stats per day, grouped 1 month per table):

Code:
    Set rG = rH.Cells(1)  'first cell in header row
    Set rG = Range(rG, rG.End(xlToRight))   '...to last cell in header row
    Set rG = Union(rG, rG.Offset(3), rG.Offset(4))   'include the two rows of data relevant to the chart
    Set nm = ActiveSheet.Names.Add("graph_01", rG)   'add the range name "graph_01"
    rG.Select     'just for debugging

So.... nm and rG and Selection all refer to "=York!$A$7:$X$7,York!$A$10:$X$11"

however when it comes to the next line....

Code:
    ActiveSheet.ChartObjects("Chart_01").Chart.SetSourceData rG, xlRows

Excel is inexplicably loading the source data range as: =York!$A$7:$X$8,York!$A$11:$X$11

If I try to specify the source data range manually, via right-click > Select Data, it makes no difference, it still changes it back!

Any clues as to why Excel is being demented? I'm assuming the fact that I'm spanning multiple ranges has something to do with it. If so is there a common fix/workaround?
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Well, I've not exactly figured out why it's happening, but it seems that unless I specify the various axis options (or amend them manually) Excel is trying to guess what type of chart it should be and where the axes should be. Specifying/correcting them all explicitly apparently fixes the issue.

Code:
With cht
    .SetSourceData Source:=Rows("91:93")
    .SeriesCollection(1).Delete
    .SeriesCollection(1).XValues = "=York!$B$91:$W$91"
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top