(已解决)Excel-VBA:向图表工作表中Excel 2010中的系列添加值时出现错误438

我试图把一些代码从工作表中收集一组数据,然后生成一个XY散点图。 每当它到达将值input到系列的行时,就会产生“运行时错误438:对象不支持此属性或方法”。

xDataRng.Address的值为"$C$8:$C$11"

我在Win 7上使用Excel 2010。

我已经查阅了每篇文章和帮助线索,我可以find关于这个问题。 我.SeriesCollection.XValues正确使用.SeriesCollection.XValues ? 我该如何解决?

谢谢,

Eeshwar

 Sub createChart() Set wb = ThisWorkbook Dim sheetName As Variant, chartName As Variant sheetName = ActiveSheet.Name 'Find x-axis data Dim xnameRng As Range, xdataRng As Range Dim lastCol As Long, lastRow As Long Dim i As Integer With ActiveSheet lastCol = .Cells(7, .Columns.Count).End(xlToLeft).Column Set xnameRng = .Range(Cells(7, 2), Cells(7, lastCol)).Find("Horizontal Position (", lookat:=xlPart) lastRow = .Cells(.Rows.Count, xnameRng.Column).End(xlUp).Row Set xdataRng = .Range(xnameRng.Offset(1, 0).Address, Cells(lastRow, xnameRng.Column)) End With 'Find y-axis data Dim ynameRng As Range, ydataRng As Range With ActiveSheet Set ynameRng = .Range(.Cells(7, 2), .Cells(7, lastCol)).Find("Pressure (", lookat:=xlPart) Set ydataRng = .Range(ynameRng.Offset(1, 0).Address, .Cells(lastRow, ynameRng.Column)) End With 'Create chart With wb.Sheets("Chart_Template") .Copy After:=Sheets(sheetName) chartName = ActiveChart.Name 'Update chart details With wb.Sheets(chartName) .SeriesCollection.NewSeries .SeriesCollection.XValues(1) = wb.Sheets(sheetName).Range(xdataRng.Address) FAILS HERE .SeriesCollection.Values(1) = wb.Sheets(sheetName).Range(ydataRng.Address) .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).Character.Text = xnameRng.Value .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).Character.Text = ynameRng.Value End With End With End Sub 

尝试下面的代码部分来取代你的:

 With wb.Sheets(chartName) Dim Ser As Series Set Ser = .SeriesCollection.NewSeries With Ser .XValues = "=" & xDataRng.Address(True, True, xlA1, xlExternal) .Values = "=" & yDataRng.Address(True, True, xlA1, xlExternal) End With .Axes(xlCategory, xlPrimary).HasTitle = True .Axes(xlCategory, xlPrimary).Character.Text = xnameRng.Value .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).Character.Text = ynameRng.Value End With