如何检查工作表中是否存在任何形状?
我使用下面的代码:
Sub my()
Dim shp As Shape
If Not shp Is Nothing Then
For Each shp In Sheet1.Shapes
mesage = shp.TopLeftCell.Address(0, 0)
Next shp
Else
mesage = Sheet1.Cells(1, 12).Address
End If
End Sub
由于我没有给出形状名称,它正在执行'if-else'循环的'else'部分。
因为每次形状名称不同,我不能在这里给出形状名称。
发布于 2019-05-21 10:07:27
如果要检查是否有任何 VBA形状活动工作表上,那么你可以简单地检查的价值.Count
属性的的Shapes
对象:
ActiveSheet.Shapes.Count
...将返回形状数量,如果没有形状则返回零。
用法示例:
If ActiveSheet.Shapes.Count = 0 Then MsgBox "No shapes found!"
如果需要检查是否存在特定形状,请使用此功能:
Function shapeExists(shapeName As String) As Boolean
'returns TRUE if a shape named [ShapeName] exists on the active worksheet
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
If sh.Name = shapeName Then shapeExists = True
Next sh
End Function
用法示例:
If Not shapeExists("My Shape Name") Then MsgBox "Shape not found!"
在“立即”窗口中列出活动工作表上的所有形状。(按Ctrl+ G打开它。)
Sub ListAllShapes()
'list all shapes on the active worksheet
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
Debug.Print "id=" & sh.ID, "name=" & sh.Name
Next sh
End Sub
从活动工作表中删除所有形状。
Sub DeleteAllShapes()
'delete all shapes on the active worksheet (Including CONTROLS, so use with caution!)
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
sh.Delete
Next sh
End Sub
有关使用VBA形状的更多详细信息和示例,请参阅我对其他与形状相关的问题的答案(包括仅为另一种形状的控件):
https://stackoverflow.com/questions/-100005180
复制相似问题