Excel 要清除特定范围内的形状,但存在应用程序错误

Excel 要清除特定范围内的形状,但存在应用程序错误,excel,vba,runtime-error,shapes,Excel,Vba,Runtime Error,Shapes,基本上,我正在做一个代码,它将根据某个变量显示形状。但是,一旦某个变量发生更改,就会出现“运行时错误“1004”:应用程序定义的错误或对象定义的错误”。我想创建一个模块,为按钮分配一个宏;想要清除特定范围内的形状,但有错误。但是,一旦我重置并调试了模块,它就可以正常工作了。尽管如此,如果某个变量发生变化,问题再次出现 Sub ClearingofButton() Dim pic As Picture Dim shp As Shape ActiveSheet.Unprotect If Shee

基本上,我正在做一个代码,它将根据某个变量显示形状。但是,一旦某个变量发生更改,就会出现“运行时错误“1004”:应用程序定义的错误或对象定义的错误”。我想创建一个模块,为按钮分配一个宏;想要清除特定范围内的形状,但有错误。但是,一旦我重置并调试了模块,它就可以正常工作了。尽管如此,如果某个变量发生变化,问题再次出现

Sub ClearingofButton()
Dim pic As Picture
Dim shp As Shape

ActiveSheet.Unprotect

If Sheets("Calculator").Range("AU64").Formula = "5" Then
    If ActiveSheet.Shapes.Count > 0 Then
    For Each shp In Sheets("Calculator").Shapes
    Application.EnableCancelKey = xlDisabled
        If Not Application.Intersect(shp.TopLeftCell, ActiveSheet.Range("Illustration")) Is Nothing Then
            shp.Delete
    Application.EnableCancelKey = xlInterrupt
        End If

    Next shp

    End If
End If

If Sheets("Calculator").Range("AU64").Formula = "10" Then
    If ActiveSheet.Shapes.Count > 0 Then
    For Each shp In Sheets("Calculator").Shapes
    Application.EnableCancelKey = xlDisabled
        If Not Application.Intersect(shp.TopLeftCell, ActiveSheet.Range("Illustration")) Is Nothing Then
            shp.Delete
    Application.EnableCancelKey = xlInterrupt
        End If

    Next shp

    End If
End If

If Sheets("Calculator").Range("AU64").Formula = "19" Then
    If ActiveSheet.Shapes.Count > 0 Then
    For Each shp In Sheets("Calculator").Shapes
    Application.EnableCancelKey = xlDisabled
        If Not Application.Intersect(shp.TopLeftCell, ActiveSheet.Range("Illustration")) Is Nothing Then
            shp.Delete
    Application.EnableCancelKey = xlInterrupt
        End If

    Next shp

    End If
End If

If Sheets("Calculator").Range("AU64").Formula = "30" Then
    If ActiveSheet.Shapes.Count > 0 Then
    For Each shp In Sheets("Calculator").Shapes
    Application.EnableCancelKey = xlDisabled
        If Not Application.Intersect(shp.TopLeftCell, ActiveSheet.Range("Illustration")) Is Nothing Then
            shp.Delete
    Application.EnableCancelKey = xlInterrupt
        End If

    Next shp

    End If
End If

If Sheets("Calculator").Range("AU64").Formula = "40" Then
    If ActiveSheet.Shapes.Count > 0 Then
    For Each shp In Sheets("Calculator").Shapes
    Application.EnableCancelKey = xlDisabled
        If Not Application.Intersect(shp.TopLeftCell, ActiveSheet.Range("Illustration")) Is Nothing Then
            shp.Delete
    Application.EnableCancelKey = xlInterrupt
        End If

    Next shp

    End If
End If

End Sub

请尝试此代码,看看是否会引发相同的问题

Sub ClearingOfButton()

    Dim Ws As Worksheet
    Dim Shp As Shape
    Dim Tmp As Variant

    Set Ws = ActiveSheet
    If Ws.Shapes.Count Then
        Ws.Unprotect
        Tmp = Sheets("Calculator").Range("AU64").Value

        If (Tmp = 5) Or (Tmp = 10) Or (Tmp = 19) Or (Tmp = 30) Or (Tmp = 40) Then
            For Each Shp In Sheets("Calculator").Shapes
                If Not Application.Intersect(Shp.TopLeftCell, _
                                             Ws.Range("Illustration")) Is Nothing Then
                    Shp.Delete
                End If
            Next Shp
        End If
    End If
End Sub

如果是这样,请在
Shp.Delete
之前添加一行
On Error Resume Next
,并调查未被删除的形状(尽管您预期它会被删除)。

在哪一行出现错误?