简体   繁体   English

检查是否存在自动整形

[英]Check if Autoshape exists

I am trying to create a simple macro to delete some data including an Autoshape - Shapes("ACCESS") , but I would like this macro to run only if Autoshape exists. 我试图创建一个简单的宏来删除一些数据,包括Autoshape- Shapes("ACCESS") ,但是我希望仅在存在Autoshape的情况下运行此宏。

If it doesn't exist, the action would be just Range("B2").Select 如果不存在,则动作将只是Range("B2").Select

Can anybody help please? 有人可以帮忙吗?

My Code 我的密码

Sub DeleteRepWT()

ActiveSheet.Shapes("ACCESS").Select
Selection.ShapeRange.IncrementLeft 57#
Selection.ShapeRange.IncrementTop -85.5
Range("M4").Select
ActiveSheet.Shapes("ACCESS").Select
Selection.Delete
Columns("K:AI").Select
Selection.Delete Shift:=xlToLeft
Range("B2").Select

End Sub

You can use an object type shape, in my code it's Shp , and try to set it to Set Shp = ActiveSheet.Shapes("ACCESS") . 您可以使用对象类型的形状,在我的代码中为Shp ,并尝试将其设置为Set Shp = ActiveSheet.Shapes("ACCESS")

Add On Error Resume Next before trying to Set the shape object (so won't get a run-time error), and after trying to Set the shape, you can check If Shp Is Nothing , which means checking if you were able to Set the shape (if you weren't, it means there is no Shapes("ACCESS") in your worksheet). 添加On Error Resume Next在尝试Set形状对象之前(因此不会出现运行时错误),然后在尝试Set形状之后,可以检查If Shp Is Nothing ,这意味着检查是否可以Set形状(如果不是,则意味着工作表中没有Shapes("ACCESS") )。

Code

Option Explicit

Sub DeleteRepWT()

Dim Shp As Shape

On Error Resume Next
Set Shp = ActiveSheet.Shapes("ACCESS") ' try to set the object to "ACCESS"
On Error GoTo 0

If Shp Is Nothing Then ' if there is no Shapes("ACCESS")
    Range("B2").Select 
Else ' shape exists
    With Shp
        ' rest of your original code here ...

    End With
End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM