Excel精英培训网

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

[已解决]遍历删除问题

[复制链接]
发表于 2014-11-20 22:17 | 显示全部楼层 |阅读模式
遍历删除问题,出错的。
最佳答案
2014-11-20 22:36
  1. Sub 遍历整个工作簿将包含3A的整行删除()
  2. '就是说整个工作簿,只要哪一个单元格包含3A,就将其整行删除。

  3. 'Find方法:MatchCase  Variant 类型,可选。若为 True,则进行区分大小写的查找。默认值为 False!

  4.     Dim c As Range, rng As Range, sh As Worksheet, firstAddress$
  5.     For Each sh In Sheets
  6.         Set rng = Nothing
  7.         With sh.UsedRange
  8.             Set c = .Find("3A", , , 1, , , True)
  9.             If Not c Is Nothing Then
  10.                 firstAddress = c.Address
  11.                 Do
  12.                     If rng Is Nothing Then Set rng = c.EntireRow Else Set rng = Union(rng, c.EntireRow)
  13.                     Set c = .FindNext(c)
  14.                 Loop While Not c Is Nothing And c.Address <> firstAddress
  15.             End If
  16.         End With
  17.         If Not rng Is Nothing Then
  18.            rng.Delete
  19.         End If
  20.     Next
  21. End Sub
复制代码
不能在重叠区域上使用此命令.jpg

遍历删除问题.rar

7.65 KB, 下载次数: 10

发表于 2014-11-20 22:36 | 显示全部楼层    本楼为最佳答案   
  1. Sub 遍历整个工作簿将包含3A的整行删除()
  2. '就是说整个工作簿,只要哪一个单元格包含3A,就将其整行删除。

  3. 'Find方法:MatchCase  Variant 类型,可选。若为 True,则进行区分大小写的查找。默认值为 False!

  4.     Dim c As Range, rng As Range, sh As Worksheet, firstAddress$
  5.     For Each sh In Sheets
  6.         Set rng = Nothing
  7.         With sh.UsedRange
  8.             Set c = .Find("3A", , , 1, , , True)
  9.             If Not c Is Nothing Then
  10.                 firstAddress = c.Address
  11.                 Do
  12.                     If rng Is Nothing Then Set rng = c.EntireRow Else Set rng = Union(rng, c.EntireRow)
  13.                     Set c = .FindNext(c)
  14.                 Loop While Not c Is Nothing And c.Address <> firstAddress
  15.             End If
  16.         End With
  17.         If Not rng Is Nothing Then
  18.            rng.Delete
  19.         End If
  20.     Next
  21. End Sub
复制代码
回复

使用道具 举报

发表于 2014-11-20 22:43 | 显示全部楼层
本帖最后由 Excel学徒123 于 2014-11-20 22:44 编辑

  1. Sub 删除3A所在行()
  2.     Dim sh As Worksheet, rg As Range
  3.         For Each sh In Worksheets
  4.             With sh
  5.                 For Each rg In .UsedRange
  6.                     DoEvents
  7.                     If rg = "3A" Then
  8.                         rg.EntireRow.Delete
  9.                     End If
  10.                 Next
  11.             End With
  12.         Next
  13. End Sub
复制代码
用的遍历单元格的方法,你看看行不行,没用用FIND{:112:}

评分

参与人数 2 +24 收起 理由
qh8600 + 18
张雄友 + 6 速度上没有优势。

查看全部评分

回复

使用道具 举报

发表于 2014-11-21 08:25 | 显示全部楼层
Excel学徒123 发表于 2014-11-20 22:43
用的遍历单元格的方法,你看看行不行,没用用FIND

遍历的话最好不要一行一行的删除和一行一行的写,这样影响速度的
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 03:57 , Processed in 0.376926 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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