Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4409|回复: 9

[已解决]删除对象

[复制链接]
发表于 2013-5-25 10:15 | 显示全部楼层 |阅读模式
8学分
本帖最后由 sparkguo 于 2013-5-25 10:38 编辑

按条件删除对象
详情见附件,谢谢各位大侠了。
最佳答案
2013-5-25 10:24
  1. Sub 删除对象()
  2.     Dim objShp As Shape
  3.     Dim rg As Range
  4.     Dim strMsg$
  5.     Set rg = Range("e3:f5")
  6.     For Each objShp In ActiveSheet.Shapes
  7.         If Not Application.Intersect(objShp.TopLeftCell, rg) Is Nothing Then
  8.             strMsg = strMsg & objShp.Name & vbCrLf
  9.             objShp.Delete
  10.         End If
  11.     Next
  12.     If Len(strMsg) > 0 Then
  13.         MsgBox "删除的对象如下:" & vbCrLf & strMsg
  14.     Else
  15.         MsgBox "没有可删除的对象"
  16.     End If
  17.    
  18. End Sub
复制代码

图片 - 副本.rar

270.21 KB, 下载次数: 11

发表于 2013-5-25 10:22 | 显示全部楼层
  1. Sub 删除对象()
  2.     Dim objShp As Shape
  3.     Dim rg As Range
  4.     Set rg = Range("e3:f5")
  5.     For Each objShp In ActiveSheet.Shapes
  6.         If Not Application.Intersect(objShp.TopLeftCell, rg) Is Nothing Then
  7.             objShp.Delete
  8.         End If
  9.     Next
  10. End Sub
复制代码
回复

使用道具 举报

发表于 2013-5-25 10:24 | 显示全部楼层    本楼为最佳答案   
  1. Sub 删除对象()
  2.     Dim objShp As Shape
  3.     Dim rg As Range
  4.     Dim strMsg$
  5.     Set rg = Range("e3:f5")
  6.     For Each objShp In ActiveSheet.Shapes
  7.         If Not Application.Intersect(objShp.TopLeftCell, rg) Is Nothing Then
  8.             strMsg = strMsg & objShp.Name & vbCrLf
  9.             objShp.Delete
  10.         End If
  11.     Next
  12.     If Len(strMsg) > 0 Then
  13.         MsgBox "删除的对象如下:" & vbCrLf & strMsg
  14.     Else
  15.         MsgBox "没有可删除的对象"
  16.     End If
  17.    
  18. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-5-25 10:35 | 显示全部楼层
hwc2ycy 发表于 2013-5-25 10:24

If Not Application.Intersect(objShp.TopLeftCell, rg) Is Nothing
这句代码能不能详细注释一下,
回复

使用道具 举报

发表于 2013-5-25 10:37 | 显示全部楼层
sparkguo 发表于 2013-5-25 10:35
If Not Application.Intersect(objShp.TopLeftCell, rg) Is Nothing
这句代码能不能详细注释一下,

直接把对像所在的左上角单元格与区域E3:F5,通过交集方法,如果有交集返回的就是交集的单元格,否则就是空。
回复

使用道具 举报

发表于 2013-5-25 10:38 | 显示全部楼层
…………………………
回复

使用道具 举报

发表于 2013-5-25 10:38 | 显示全部楼层
Application.Intersect 方法
返回一个 Range 对象,该对象表示两个或多个区域重叠的矩形区域。
回复

使用道具 举报

发表于 2013-5-25 10:38 | 显示全部楼层
我说的交集,不如帮助里说的是重叠的部分。
回复

使用道具 举报

发表于 2013-5-25 10:55 | 显示全部楼层
本帖最后由 hwc2ycy 于 2013-5-25 10:56 编辑

增加互动,可以指定单元格区域。
  1. Sub 删除对象()
  2.     Dim objShp As Shape
  3.     Dim rg As Range
  4.     Dim strMsg$

  5.     'Set rg = Range("e3:f5")
  6.     On Error Resume Next
  7.     Do
  8.         Set rg = Application.InputBox(prompt:="请选择要删除对象的单元格区域", Title:="删除指定单元格区域内的对象", Type:=8)
  9.         Err.Clear
  10.     Loop Until Not rg Is Nothing

  11.     On Error GoTo ErrorHandler

  12.     For Each objShp In ActiveSheet.Shapes
  13.         If Not Application.Intersect(objShp.TopLeftCell, rg) Is Nothing Then
  14.             strMsg = strMsg & objShp.Name & vbCrLf
  15.             objShp.Delete
  16.         End If
  17.     Next

  18.     If Len(strMsg) > 0 Then
  19.         MsgBox "删除的对象如下:" & vbCrLf & strMsg
  20.     Else
  21.         MsgBox "没有可删除的对象"
  22.     End If
  23.     exit sub
  24. ErrorHandler:
  25.     MsgBox Err.Number & vbCrLf & _
  26.            Err.Description

  27. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-5-26 14:27 | 显示全部楼层
hwc2ycy 发表于 2013-5-25 10:55
增加互动,可以指定单元格区域。

谢谢老师   思路开拓了,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-18 16:10 , Processed in 0.323263 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表