2017-04-19 7 views
2

ワークシートから一連のデータを収集し、XYスキャタープロットを生成するコードをまとめようとしています。シリーズに値を入力する行に達するたびに、「実行時エラー438:オブジェクトがこのプロパティまたはメソッドをサポートしていません」が生成されます。Excel-VBA:グラフシート、Excel 2010の系列に値を追加する際のエラー438

xDataRng.Addressは、私は私がこの問題について見つけることができるすべての記事やヘルプスレッドを見てきたのWin 7

にエクセル2010を使用してい"$C$8:$C$11"

の値を持っています。 .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 

答えて

2

あなたを置き換えるために、以下のコードのセクションをお試しください:

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 
+0

また、 'SeriesCollection.XValues'が配列では、=作ることができます'xdataRng.Value'しかし、それは静的になります。あなたのソリューションは、それを範囲(y)に動的にリンクさせます。 –

+0

@ A.S.H最近、あまりに多くのCharts and Pivots MACROを使用していました;) –

+0

Thanks Shai!それは完璧に働いた。 '.Axes(xlCategory、xlPrimary).Character.Text'は動作しませんが、' .Axes(xlCategory、xlPrimary).TextTitle.Text'は動作します。 – Eeshwar

関連する問題