想要清除一定范围内的形状,但存在应用错误

问题描述 投票:0回答:1

基本上,我正在编写代码,它将根据某个变量显示形状。但是,一旦某些变量更改,将出现“运行时错误'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
excel vba runtime-error shapes
1个回答
0
投票

请尝试使用此代码,看看是否引起同样的问题。

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

如果有,则在On Error Resume Next之前添加Shp.Delete行,并调查虽然您希望这样做但不会被删除的形状。

© www.soinside.com 2019 - 2024. All rights reserved.