2015-11-27 57 views
-1

我想上的MS Excel website運行時錯誤91:對象變量或With塊變量未設置,例如,從MS辦公室網站

下面是代碼

給定提取使用宏從MS-Excel的圖形數據
Sub GetChartValues() 
    Dim NumberOfRows As Integer 
    Dim X As Object 
    Counter = 2 

    ' Calculate the number of rows of data. 
    NumberOfRows = UBound(ActiveChart.SeriesCollection(1).Values) 

    Worksheets("ChartData").Cells(1, 1) = "X Values" 

    ' Write x-axis values to worksheet. 
    With Worksheets("ChartData") 
     .Range(.Cells(2, 1), _ 
     .Cells(NumberOfRows + 1, 1)) = _ 
     Application.Transpose(ActiveChart.SeriesCollection(1).XValues) 
    End With 

    ' Loop through all series in the chart and write their values to 
    ' the worksheet. 
    For Each X In ActiveChart.SeriesCollection 
     Worksheets("ChartData").Cells(1, Counter) = X.Name 

     With Worksheets("ChartData") 
     .Range(.Cells(2, Counter), _ 
     .Cells(NumberOfRows + 1, Counter)) = _ 
     Application.Transpose(X.Values) 
     End With 


     Counter = Counter + 1 
    Next 

End Sub 

此代碼中的錯誤在哪裏?

+0

你調試代碼?它在哪一行發出錯誤? –

+0

在這條線上 'NumberOfRows = UBound(ActiveChart.SeriesCollection(1).Values)' –

+0

兩件事:你是否真的有一個標籤爲「ChartData」的工作表,你有一個活動的圖表?您需要在您提供的鏈接上遵循幾個步驟 –

回答

1

VB編譯器不會知道你在說哪個ActiveChart。因此,您需要先設置設置圖表對象,然後嘗試使用它。

檢查這個代碼:

Dim mychart As ChartObject 

Set wb = ThisWorkbook 
Set ws = wb.Sheets("Sheet1") 
Set mychart = ws.ChartObjects("Chart 1") 

With mychart.Chart 
    'do stuff here 
End With 
相關問題