如何检查是否存在任何形状?

编程入门 行业动态 更新时间:2024-10-28 02:23:28
本文介绍了如何检查是否存在任何形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何检查工作表中是否存在任何形状?

How to check if any shape exists in sheet?

我使用了以下代码:

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"部分.

As I have not given shape name, it is executing 'else' part of 'If-else' loop.

由于形状名称每次都不相同,因此我无法在此处提供形状名称.

I can't give shape name here as each time shape name is different.

推荐答案

检查是否存在任何形状

如果您要检查是否有任何 VBA Shapes 在活动工作表上,那么您只需检查的 Shapes对象 >:

Check if any Shapes exist

If you want to check whether there are any VBA Shapes on the active worksheet then you can simply check the value of the .Count property of the Shapes object:

ActiveSheet.Shapes.Count

...将返回形状的数量,如果没有形状,则返回零.

...which will return the number of shapes, or return zero if there are no shapes.

用法示例:

If ActiveSheet.Shapes.Count = 0 Then MsgBox "No shapes found!"

检查是否存在特定形状

如果需要检查是否存在 特定 形状,请使用以下功能:

Check if specific Shape exists

If you need to check whether a specific shape exists, use this function:

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 将其打开.)

List All Shapes

Lists all shapes on the active worksheet, in the Immediate window. (Hit Ctrl+G to open it.)

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

删除所有形状

从活动工作表中删除所有形状.

Delete All Shapes

Deletes all shapes from the active worksheet.

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 Shapes的更多详细信息和示例,请参阅我对其他与 Shape相关的问题(包括 control 只是Shape的另一种形式)的回答:

More Information:

For more detailed information and examples of working with VBA Shapes, see my answers to other Shape-related questions (including controls which are just another kind of Shape):

  • 粘贴的形状不被视为最新"形状
  • 基于单元格值更改excel中的弧长
  • 无法将项目添加到组合框形状
  • Excel中的窗体控件和ActiveX控件之间的差异概述
  • 如何在用户窗体上使用带有选项按钮控件的事件
  • 如何禁用形状的右键菜单
  • Pasted Shape not seen as "Latest" Shape
  • Changing arc length in excel based on a cell value
  • Can't add items to a combobox shape
  • Overview of differences between Form Controls and ActiveX Controls in Excel
  • How to use Events with Option Button Controls on UserForm
  • How to disable right click menu of shapes

更多推荐

如何检查是否存在任何形状?

本文发布于:2023-11-27 15:43:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638609.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   形状

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!