Log in

View Full Version : How to define Range in non-contiguous format for xy chart?



yannezhang
02-20-2013, 09:55 AM
I am writing VBA code for an x-y chart using data from columns A and C. A provides data for x-axis, C for y-axis in the example below:

A B C
1 10 20
2 11 21
3 12 22
4 13 23

The following lines work correctly with the Range format:

With ActiveChart
.ChartType = xlXYScatterLines
SetSourceData Source:=Sheets("sorted").Range("a2:a5, c2:c5"), PlotBy:=xlColumns
End With

But I run into difficulty composing the same line with variables, i.e 2->FirstCell, 5->LastCell, and not include column B.

Things I've tried:
.SetSourceData Source:=Sheets("sorted").Range("a" & FirstCell, "c" & LastCell), PlotBy:=xlColumns
(This covers all columns A,B and C. But I want A and C only)

Dave
02-21-2013, 01:42 PM
You could re-arrange your data or you can delete the first series. It seems silly that you can't just chart what you want. Here's the code. HTH. Dave

ActiveChart.SeriesCollection(1).Delete