I'm trying to set the data set for a chart object using a macro. The macro selects the correct range that I want (I check by using rng.Select and debugging) but when I right click the chart object after the macro has finished running and select Select Data it says the data range is too complex to display and does not correctly populate my chart. My macro is below. Any pointers appreciated.
EDIT. I have a copy of the sheet where the graph was created from the populated data manually and clicking Select Data on this chart shows the non-contiguous range just fine - it's only when trying to set it by macro for a pre-existing chart that it doesn't work.
Sub test()
UpdateChart 27, 64
End Sub
Sub UpdateChart(ByVal row As Long, ByVal col As Long)
Dim sht As Worksheet
Set sht = Worksheets("Report4_Chart")
Dim data As Worksheet
Set data = Worksheets("Report4")
Dim rng As Range
Dim exclude As Range
data.Activate
Set exclude = data.Rows(25)
Set rng = data.Range("A24", Intersect(data.Rows(row), data.Columns(col)))
Set rng = SetDifference(rng, exclude)
rng.Select
sht.Activate
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.PlotArea.Select
ActiveChart.SetSourceData Source:=rng
End Sub
For anyone else who's stumbled across this old question: I just discovered that basing a chart on non-contiguous ranges seems to work only if the ranges form a rectangle.
For instance, I was trying to group cells A1, A7, and C1, and couldn't get it to work. However, when I used A1, C1, A7, and C7, it did work.
(Actually, my ranges were all larger than a single cell. But the "missing" range was the same width as the one above it, and the same height as the one to the side of it. So the combined ranges formed a rectangle.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.