以下代码成功生成具有动态变化范围的XY散点图。但是,我无法克服的障碍是如何以编程方式设置散点图的x,y,z值以生成气泡图。我已尝试这样做(正如您在代码中看到的那样),但它无法正常工作。
感谢任何帮助
dim a as long
dim b as long
dim i as long
dim m as long
dim j as long
For i = 2 To lastrow
If Cells(i, "A") = "Regression #" & m & " Output" Then
a = i 'save regression #1 row number, where it begins
For j = 2 To a
If Cells(j, "A") = "Regression #" & m + 1 & " Output" Then
b = j
End If
Next j
Set rngx = Range(Cells(b + 1, 2), Cells(a - 1, 2))
Set rngy = Range(Cells(b + 1, 7), Cells(a - 1, 7))
Set rngz = Range(Cells(b + 1, 8), Cells(a - 1, 8))
Sheets("Chart Table").Shapes.AddChart.Select
ActiveChart.ChartType = xlBubble
ActiveChart.SetSourceData Source:=Union(rngx, rngy, rngz)
With ActiveChart.SeriesCollection.NewSeries
'.Name = ActiveSheet.Range("B2:B13")
.value = rngy
.xvalues = rngx
.BubbleSizes = rngz
End With
With ActiveChart
.SetElement (msoElementChartTitleAboveChart)
.Legend.Delete
.ChartTitle.Select
.ChartTitle.Text = Cells(a, 1).Value
End With
Else
End If
Next i
例如,如果我只有一行数据(一个x,一个y,一个z) - 那么我希望Excel知道它意味着只有一个气泡。现在它将它读作3个独立的气泡,无视那里有泡沫尺寸元素的事实
答案 0 :(得分:1)
如果rngx,rngy和rngz的并集是一个连续的范围,它可以正常工作。由于这些范围由多列分隔,因此您的SetSourceData命令不完整,并将其union作为一组Y值处理,并使用默认值(1,2,3)作为X值和(1,1,1)作为气泡大小。
然而,这个小改动修复了你的代码:
ActiveChart.SetSourceData Source:=Union(rngx, rngy, rngz), PlotBy:=xlColumns