Excel 运行时错误91对象变量或未设置块变量

Excel 运行时错误91对象变量或未设置块变量,excel,vba,Excel,Vba,这个简单的代码得到了这个错误。 为什么? 结束SubA图表/图表对象调查 (OP)设置只是一个带有嵌入式图表(对象)的工作表 下面演示了编写代码的各种方法。除了最后一个测试激活的程序外,所有程序都执行相同的操作。选择其中一个或创建另一个 这只是一个对象调查,因为我对图表一无所知,也不想查看文档。您可能应该在图表、工作表、图纸、图表、图表对象、轴和轴上进行调查 代码 Option Explicit Sub TestChart() Dim ch As Chart: Set ch = A

这个简单的代码得到了这个错误。 为什么?

结束Sub

A图表/图表对象调查
  • (OP)设置只是一个带有嵌入式图表(对象)的工作表
  • 下面演示了编写代码的各种方法。除了最后一个测试激活的程序外,所有程序都执行相同的操作。选择其中一个或创建另一个
  • 这只是一个对象调查,因为我对图表一无所知,也不想查看文档。您可能应该在图表、工作表、图纸、图表、图表对象、轴和轴上进行调查
代码

Option Explicit

Sub TestChart()
    Dim ch As Chart: Set ch = ActiveSheet.ChartObjects("Chart 1").Chart
    With ch.Axes(xlCategory, xlPrimary)
        .MaximumScale = ActiveSheet.Range("A33").Value
    End With
End Sub

Sub TestChartObject()
    Dim cho As ChartObject: Set cho = ActiveSheet.ChartObjects("Chart 1")
    With cho.Chart.Axes(xlCategory, xlPrimary)
        .MaximumScale = ActiveSheet.Range("A33").Value
    End With
End Sub

Sub testAxesActiveSheet()
    With ActiveSheet.ChartObjects("Chart 1").Chart.Axes(xlCategory, xlPrimary)
        .MaximumScale = ActiveSheet.Range("A33").Value
    End With
End Sub

Sub testAxesTripleParent()
    With ActiveSheet.ChartObjects("Chart 1").Chart.Axes(xlCategory, xlPrimary)
        .MaximumScale = .Parent.Parent.Parent.Range("A33").Value
    End With
End Sub

Sub testAxes()
    With ActiveSheet
        Dim axs As Axis
        Set axs = .ChartObjects("Chart 1").Chart.Axes(xlCategory, xlPrimary)
        axs.MaximumScale = .Range("A33").Value
    End With
End Sub

Sub testAxesChart()
    With ActiveSheet
        Dim ch As Chart: Set ch = .ChartObjects("Chart 1").Chart
        Debug.Print ch.Name ' Result?: 'Sheet1 Chart 1'
        Dim axs As Axis: Set axs = ch.Axes(xlCategory, xlPrimary)
        axs.MaximumScale = .Range("A33").Value
    End With
End Sub

Sub testAxesChartObject()
    With ActiveSheet
        Dim cho As ChartObject: Set cho = .ChartObjects("Chart 1")
        Debug.Print cho.Name ' Result: 'Chart 1'
        Dim axs As Axis: Set axs = cho.Chart.Axes(xlCategory, xlPrimary)
        axs.MaximumScale = .Range("A33").Value
    End With
End Sub

Sub TestActiveChart()
    On Error GoTo SheetError
    Debug.Print "ActiveSheet Name: " & ActiveSheet.Name
    On Error GoTo ChartError
    Debug.Print "ActiveChart Name: " & ActiveChart.Name
ProcExit:
    Exit Sub
SheetError:
    Debug.Print "No active sheet."
    Resume Next
ChartError:
    Debug.Print "No active chart."
    Resume Next
End Sub

根据错误发生在哪一行:如果是带有…的
行,则工作表处于活动状态;如果是
.MaximumScale…
行,则
ActiveChart
当前是
ActiveSheet
并且没有
范围
属性。请注意,没有
activesheet
属性。我只有一个工作表和一个图表。不能是活动的吗?这发生在with线上。什么是“工作表处于活动状态”?谢谢。现在我得到“对象不支持此属性或方法”这是关于VBA或VBScript的问题吗?请使用正确的标签。
Option Explicit

Sub TestChart()
    Dim ch As Chart: Set ch = ActiveSheet.ChartObjects("Chart 1").Chart
    With ch.Axes(xlCategory, xlPrimary)
        .MaximumScale = ActiveSheet.Range("A33").Value
    End With
End Sub

Sub TestChartObject()
    Dim cho As ChartObject: Set cho = ActiveSheet.ChartObjects("Chart 1")
    With cho.Chart.Axes(xlCategory, xlPrimary)
        .MaximumScale = ActiveSheet.Range("A33").Value
    End With
End Sub

Sub testAxesActiveSheet()
    With ActiveSheet.ChartObjects("Chart 1").Chart.Axes(xlCategory, xlPrimary)
        .MaximumScale = ActiveSheet.Range("A33").Value
    End With
End Sub

Sub testAxesTripleParent()
    With ActiveSheet.ChartObjects("Chart 1").Chart.Axes(xlCategory, xlPrimary)
        .MaximumScale = .Parent.Parent.Parent.Range("A33").Value
    End With
End Sub

Sub testAxes()
    With ActiveSheet
        Dim axs As Axis
        Set axs = .ChartObjects("Chart 1").Chart.Axes(xlCategory, xlPrimary)
        axs.MaximumScale = .Range("A33").Value
    End With
End Sub

Sub testAxesChart()
    With ActiveSheet
        Dim ch As Chart: Set ch = .ChartObjects("Chart 1").Chart
        Debug.Print ch.Name ' Result?: 'Sheet1 Chart 1'
        Dim axs As Axis: Set axs = ch.Axes(xlCategory, xlPrimary)
        axs.MaximumScale = .Range("A33").Value
    End With
End Sub

Sub testAxesChartObject()
    With ActiveSheet
        Dim cho As ChartObject: Set cho = .ChartObjects("Chart 1")
        Debug.Print cho.Name ' Result: 'Chart 1'
        Dim axs As Axis: Set axs = cho.Chart.Axes(xlCategory, xlPrimary)
        axs.MaximumScale = .Range("A33").Value
    End With
End Sub

Sub TestActiveChart()
    On Error GoTo SheetError
    Debug.Print "ActiveSheet Name: " & ActiveSheet.Name
    On Error GoTo ChartError
    Debug.Print "ActiveChart Name: " & ActiveChart.Name
ProcExit:
    Exit Sub
SheetError:
    Debug.Print "No active sheet."
    Resume Next
ChartError:
    Debug.Print "No active chart."
    Resume Next
End Sub