我正在尝试编写一些VBA,它会自动填充散点图,使得表中的每一行(x,y)代表一个系列,即每个系列一个点。到目前为止,我附上了我的代码。但是,当我来运行它时失败并出现以下运行时错误
“运行时错误'438':
Object不支持此属性或方法。
Sub CreateChart()
Dim NPOINTS As Integer
Dim NVAL(1000) As Range, XVAL(1000) As Range, YVAL(1000) As Range
Sheets("Scenario").Select
Range("B4").Select
NPOINTS = Worksheets("Scenario").Range(Selection, Selection.End(xlDown)).Rows.Count
Set Scenario = Worksheets("Scenario")
ActiveSheet.Shapes.AddChart2(240, xlXYScatter).Select
NVAL0 = "B3"
XVAL0 = "C3"
YVAL0 = "D3"
For i = 1 To NPOINTS
Set NVAL(i) = Cells(Range(NVAL0).Offset(i, 0).Row, Range(NVAL0).Column)
Set XVAL(i) = Cells(Range(XVAL0).Offset(i, 0).Row, Range(XVAL0).Column)
Set YVAL(i) = Cells(Range(YVAL0).Offset(i, 0).Row, Range(YVAL0).Column)
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(i).Name = NVAL(i)
ActiveChart.FullSeriesCollection(i).XValues = XVAL(i)
ActiveChart.FullSeriesCollection(i).YValues = YVAL(i)
Next
End Sub
我哪里错了?
答案 0 :(得分:1)
散点图中的y值存储在Values
中,而不是YValues
中,因此您需要相应地更改最后一行。