检查自选图形是否存在
Check if Autoshape exists
我正在尝试创建一个简单的宏来删除一些数据,包括自选图形
- Shapes("ACCESS")
,但我希望此宏仅在自选图形存在时为 运行。
如果不存在,则操作只是 Range("B2").Select
有人可以帮忙吗?
我的代码
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
你可以使用对象类型的形状,在我的代码中它是Shp
,并尝试将其设置为Set Shp = ActiveSheet.Shapes("ACCESS")
。
在尝试 Set
形状对象之前添加 On Error Resume Next
(因此不会出现 run-time 错误),并且在尝试 Set
形状之后,您可以检查 If Shp Is Nothing
,这意味着检查您是否能够 Set
形状(如果不能,则意味着您的工作表中没有 Shapes("ACCESS")
)。
代码
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
我正在尝试创建一个简单的宏来删除一些数据,包括自选图形
- Shapes("ACCESS")
,但我希望此宏仅在自选图形存在时为 运行。
如果不存在,则操作只是 Range("B2").Select
有人可以帮忙吗?
我的代码
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
你可以使用对象类型的形状,在我的代码中它是Shp
,并尝试将其设置为Set Shp = ActiveSheet.Shapes("ACCESS")
。
在尝试 Set
形状对象之前添加 On Error Resume Next
(因此不会出现 run-time 错误),并且在尝试 Set
形状之后,您可以检查 If Shp Is Nothing
,这意味着检查您是否能够 Set
形状(如果不能,则意味着您的工作表中没有 Shapes("ACCESS")
)。
代码
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