excel - 使用 Excel VBA 命令删除除特定 ShapeType 之外的所有形状

标签 excel vba

我有用焊缝标记的零件图。每张图都是相同的,但具有唯一的序列号。每个焊缝均由圆形 (msoShapeOval) 表示。在我点击保存功能后,我希望从绘图中删除所有圆形(并且仅圆形),以便我可以开始下一个序列号。

我尝试使用带有 Shape.Type 命令的 If 语句来删除所有 msoShapeOval,但该文件无法识别形状。

我还尝试了相反的方法,即“如果不是”(文本框、OLE、图片等)删除形状,但是删除了所有形状。

Sub DeleteAllWelds()
Dim shp As Shape
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")

For Each shp In ws.Shapes
    If shp.Type = msoShapeOval Then shp.Delete
Next shp

End Sub

这是调用DeleteWelds 子项的CommandButton

Private Sub CommandButton2_Click() 'Save and create new Weld Map for same Part Number
'Remove all weld shapes 'Reset any counters 'Reset fields in UserForm1 but not all

Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
Call SaveDoc
    If UserForm3_Exit.Tag = "Go" Then
        ws.Range("I2, I3, A6:K80").Value = ""
        Call DeleteAllWelds
        Unload Me
    ElseIf UserForm3_Exit.Tag = "Cancel" Then
        Unload Me
    End If

结束子

最后是创建椭圆形的子。

Sub ShapeWithNum()
    ActiveSheet.Shapes.AddShape(msoShapeOval, 20, 40, 20, 20).Select
    Selection.ShapeRange.TextFrame2.TextRange.ParagraphFormat.Alignment = _
        msoAlignCenter
    Selection.ShapeRange.TextFrame2.VerticalAnchor = msoAnchorMiddle
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = buttonCell
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 1). _
        ParagraphFormat
        .FirstLineIndent = 0
        .Alignment = msoAlignCenter
    End With
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 1).Font
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.ObjectThemeColor = msoThemeColorLight1
        .Fill.ForeColor.TintAndShade = 0
        .Fill.ForeColor.Brightness = 0
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .name = "+mn-lt"
    End With
End Sub

我只删除msoShapeOval的方法不会删除任何对象,但是当我使用If not(mso(插入我需要的))时,所有对象都会被删除。

最佳答案

您需要使用shp.AutoShapeType而不是shp.Type正如@AhmedAU 在评论中评论的那样(该评论现已删除)。

Excel 中有不同种类的形状。例如3D model , AutoShape , Callout等等。您可以在 MsoShapeType enumeration (Office) 中获取整个列表。

所以当你说Shape.Type时,那么它指的是MsoShapeType 。让我们举个例子。在工作表中插入这些形状

enter image description here

现在运行此代码

Sub Sample()
    Dim ws As Worksheet
    Dim Shp As Shape

    Set ws = Sheet1

    For Each Shp In ws.Shapes
        Debug.Print "(" & Shp.Name & ")"; "---"; Shp.Type
    Next Shp
End Sub

您将得到此输出

(Oval 1)--- 1
(TextBox 2)--- 17
(Straight Connector 4)--- 9
(Right Arrow 5)--- 1
(Explosion 1 6)--- 1

所以你会注意到Oval 1 , Right Arrow 5Explosion 1 6形状类型为 1 。这意味着(如果您引用上面的链接)这些是自选图形

当您打印 msoShapeOval 的值时在立即窗口中使用 ?msoShapeOval ,您会注意到该值为 9 。绝对不是1那么这个值是多少呢?这是Shape.AutoShapeType值为 9 的属性对于 msoShapeOval

阅读Shape.AutoShapeType property (Excel) 。它表示返回或设置指定 Shape 或 ShapeRange 对象的形状类型,该对象必须表示除直线、自由图形或连接器之外的自选图形。

所以人们应该知道何时使用Shp.Type以及何时使用Shp.AutoShapeType

现在试试这个代码

Sub Sample()
    Dim ws As Worksheet
    Dim Shp As Shape

    Set ws = Sheet1

    For Each Shp In ws.Shapes
        Debug.Print "(" & Shp.Name & ")"; "--- Shape Type: "; Shp.Type; "--- AutoShape Type: "; Shp.AutoShapeType
    Next Shp
End Sub

您将看到输出为

(Oval 1)--- Shape Type:  1 --- AutoShape Type:  9
(TextBox 2)--- Shape Type:  17 --- AutoShape Type:  1
(Straight Connector 4)--- Shape Type:  9 --- AutoShape Type: -2
(Right Arrow 5)--- Shape Type:  1 --- AutoShape Type:  33
(Explosion 1 6)--- Shape Type:  1 --- AutoShape Type:  89

因此,在您的代码中,只需更改

If shp.Type = msoShapeOval

If shp.AutoShapeType = msoShapeOval

关于excel - 使用 Excel VBA 命令删除除特定 ShapeType 之外的所有形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57914619/

相关文章:

vba - 如果单元格等于产品和日期,则在相交单元格VBA中输入公式

excel - Vba用按钮上下移动行,第二行除外

c# - 如何使用c#访问excel中的隐藏列

excel - 根据文件名将Excel文件合并为一个新的Excel文件

vba - 如何更改命名范围的 RefersTo?

excel - 如何让 excel 在执行宏时显示等待消息?

windows - 打开具有通用名称部分的文件

excel - 如何使用 ExecWB 命令将页面打印为 pdf

excel - Primefaces DataExporter - XLSX 和 XLSXSTREAM

excel - 通过 TagName 进行网页抓取