Excel 运行时错误91:未设置对象变量或带块变量,例如来自MS office网站
我正在尝试使用MS excel上给出的宏从MS excel图形中提取数据 这是密码Excel 运行时错误91:未设置对象变量或带块变量,例如来自MS office网站,excel,vba,Excel,Vba,我正在尝试使用MS excel上给出的宏从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) Worksheet
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
此代码中的错误在哪里?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
你调试代码了吗?在哪一行给出错误?在这一行“NumberOfRows=UBound(ActiveChart.SeriesCollection(1).Values)”有两件事:您是否确实有一个标记为“ChartData”的工作表,以及是否有一个处于活动状态的图表?在您提供的链接上,您需要遵循以下几个步骤是的,我有标有“ChartData”的工作表。我用@Paresh给出的方法来代替activeChart。