2
我正在嘗試將一些代碼從工作表中收集一組數據,然後生成一個XY散點圖。只要它到達將值輸入到系列中的行,它就會產生「運行時錯誤438:對象不支持此屬性或方法」。Excel-VBA:在圖表工作表中向Excel中的系列添加值時出現錯誤438
xDataRng.Address
有"$C$8:$C$11"
我使用Excel 2010在Win 7
的值,我擡頭一看每一篇文章和幫助線程我能找到這個問題。我錯誤地使用了.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
此外,'SeriesCollection.XValues'是一個數組,可以作出= 'xdataRng.Value',但這會使它變成靜態的。您的解決方案使其動態鏈接到範圍(y)。 –
@ A.S.H我最近一直使用太多圖表和樞軸MACRO;) –
謝謝Shai!這工作完美。 另外,'.Axes(xlCategory,xlPrimary).Character.Text'將不起作用,但'.Axes(xlCategory,xlPrimary).TextTitle.Text'會。 – Eeshwar