首页 > 解决方案 > 删除整个工作簿中的元素

问题描述

继续上一个查询:

在整个工作簿中复制元素

我想去掉我的形状。

为此,我使用了以下代码:

     Sub asbuilremove()
        Dim shp As Shape, sh As Worksheet
        Set ass = sh.Shapes("AsBuiltBox")
        For Each sh In ThisWorkbook.Sheets
           For Each shp In sh.Shapes
              ass.Delete
           Next
        Next
     End Sub

根据此处的示例:

https://officetricks.com/vba-macro-delete-shapes-excel-workbook-objects-boxes/

但我收到错误: 运行时错误 5 无效的过程调用或参数

那么如何删除我之前创建的 texbox(object) 呢?

在此处输入图像描述

标签: excelvba

解决方案


尝试以下操作:

Dim shp As Shape, sh As Worksheet
For Each sh In ThisWorkbook.Worksheets 'loop through all worksheets
    On Error Resume Next 'disable error reporting (next line will throw an error if AsBuiltBox does not exist)
    Set shp = sh.Shapes("AsBuiltBox")
    On Error GoTo 0 'always re-enable error reporting

    'if AsBuiltBox exists delete it
    If Not shp Is Nothing Then
        shp.Delete
    End If
Next sh

它将删除AsBuiltBox所有工作表上命名的形状(如果存在)。


推荐阅读