You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过VBA删除Excel中名称包含Picture的图片?

批量删除Excel中名称包含“Picture”的图片

我有一段用于在Excel中插入浮动图片的代码。每次新增条目时,都需要手动选中并删除原有图片,才能让当前代码添加新的图片。

注意到每次添加图片时,Excel都会为图片分配一个新的名称(例如:Picture 2、Picture 5、Picture 21等)。因此希望实现查找名称包含“Picture”字符串的图片并将其删除,为此编写了如下VBA代码:

Sub Delete_Pictures_ByName()
    
    Dim shp As Shape
    Dim picName As String
    
    picName = "Picture "
        
    For Each shp In ActiveSheet.Shapes
        If shp.Type = msoPicture Then
            If shp.Name Like "*" & picName & "*" Then
                .Delete
            End If
        End If
    Next shp
End Sub

代码错误修正

这段代码存在一处语法错误:.Delete 前缺少对象引用,必须指定要删除的形状对象 shp,修改后才能正常执行。

修正后的完整代码:

Sub Delete_Pictures_ByName()
    
    Dim shp As Shape
    Dim picName As String
    
    picName = "Picture "
        
    For Each shp In ActiveSheet.Shapes
        If shp.Type = msoPicture Then
            If shp.Name Like "*" & picName & "*" Then
                shp.Delete ' 此处添加shp对象引用
            End If
        End If
    Next shp
End Sub

补充说明

  • 代码会遍历当前活动工作表中的所有形状,筛选出类型为图片(msoPicture)且名称包含“Picture ”的对象并删除。
  • 如果需要针对特定工作表操作,可将 ActiveSheet 替换为具体工作表名称,例如 ThisWorkbook.Worksheets("Sheet1")。

内容的提问来源于stack exchange,提问作者Domenic Vitale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 21:14:50