如何使用VBA根据条件删除Excel中的行?
我目前正在构建一个宏来格式化数据表并删除不适用的数据行。具体来说,我希望删除 Column L = "ABC" 的行以及删除 Column AA <> 的行。 “DEF”。
到目前为止,我已经实现了第一个目标,但还没有实现第二个目标。现有代码是:
Dim LastRow As Integer
Dim x, y, z As Integer
Dim StartRow, StopRow As Integer
For x = 0 To LastRow
If (Range("L1").Offset(x, 0) = "ABC") Then
Range("L1").Offset(x, 0).EntireRow.Delete
x = x - 1
End If
I am currently building a macro to format a sheet of data as well as to remove inapplicable rows of data. Specifically, I am looking to delete rows where Column L = "ABC" as well as delete rows where Column AA <> "DEF".
So far I have been able to achieve the first objective, but not the second. The existing code is:
Dim LastRow As Integer
Dim x, y, z As Integer
Dim StartRow, StopRow As Integer
For x = 0 To LastRow
If (Range("L1").Offset(x, 0) = "ABC") Then
Range("L1").Offset(x, 0).EntireRow.Delete
x = x - 1
End If
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常使用自动筛选比循环范围要快得多
下面的代码创建一个工作列,然后使用公式来检测删除条件,然后自动筛选并删除结果记录
工作列放置一个公式
=OR(L1= “ABC”,AA1<>“DEF”)
到第一个空白列的第 1 行,然后向下复制到真正使用的范围。然后使用自动过滤器快速删除任何 TRUE 记录
It is normally much quicker to use AutoFilter rather than loop Ranges
The code below creates a working column, then use a formula to detect delete criteria and then autofilter and delete the result records
The working column puts a formula
=OR(L1="ABC",AA1<>"DEF")
into row 1 of the first blank column then copies down as far ar the true used range. Then any TRUE records are quicklly deleted with AutoFilter
使用循环:
使用自动过滤器(可能更快):
Using a loop:
Using autofilter (probably faster):
编号 12 的单元格为“L”,编号 27 的单元格为“AA”
Cell with number 12 is "L" and number 27 is "AA"