根据条件删除指定的行
下面是根据D列删除等如0值的行,
Set MyRange = Columns( "d ")
On Error GoTo 0
MatchString = 0
Set C = MyRange.Find(What:=MatchString, After:=MyRange.Cells(1), LookIn:=xlValues, Lookat:=xlWhole)
If Not C Is Nothing Then
Set DelRange = C
FirstAddress = C.Address
Do
Set C = MyRange.FindNext(C)
Set DelRange = Union(DelRange, C)
Loop While FirstAddress <> C.Address
End If
If Not DelRange Is Nothing Then DelRange.EntireRow.Delete
-------------------
但是我要根据两个条件,A列的值和D列等如0值的行
下面的代码:
for r = 2 to 20
if range( "a "&r) = "a列的条件值 " and range( "d "&r) = 0 then
row(r).delete
next r
这段代码不能删除所有符合条件的行
只能跳一行删除,不知为什么?
请高师指点
[解决办法]
For r = 2 To 20
If Range( "a " & r) = "a列的条件值 " And Range( "d " & r) = 0 Then
Rows(r).EntireRow.Delete
r = r - 1 '因为满足条件的行被删除后,未删之前的第r+1行变成了r行,所以不加这句的结果是满足条件被删除的行紧跟的那行被跳过了。
End If
Next r
[解决办法]
倒过来:
for r = 20 to 2 step -1
if range( "a "&r) = "a列的条件值 " and range( "d "&r) = 0 then
row(r).delete
next r