找到特定条件时删除行

时间:2017-03-31 05:38:33

标签: excel-vba vba excel

我正在尝试删除符合条件的行。

我有一个excel工作簿,名为“Calls closed” 在列“I”下拉列表中有许多条件,我想要删除匹配以下条件的所有行。

“银行关闭”,“关闭请求者”,“请求者放弃”

我尝试下面的代码,它不适合我

 Sub Callsclosed_DeleteRow()

 "Closed by bank", "With Requestor For Closure" ,"Abandoned by Requestor"

Dim LR As Long, i As Long
Dim wrksht As Excel.Worksheet

Set wrksht = Application.Worksheets("Calls closed")

LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 20000 Step -1
    If IsError(Application.Match(Range("L" & i).Value, Array("Closed By Bank", "With Requestor For Closure", "Abandoned by Requestor"), 0)) Then
 Rows(i).Delete
    Next i
    End Sub

1 个答案:

答案 0 :(得分:0)

为了删除数组的所有匹配项,您需要查找If Not IsError(Application.Match...,这意味着在数组中找到当前的单元格值。

代码

Option Explicit

Sub Callsclosed_DeleteRow()
' "Closed by bank", "With Requestor For Closure" ,"Abandoned by Requestor"

Dim LR As Long, i As Long
Dim WrkSht As Excel.Worksheet

Application.ScreenUpdating = False

Set WrkSht = Application.Worksheets("Calls closed")
With WrkSht
    LR = .Range("L" & .Rows.Count).End(xlUp).Row
    For i = LR To 1 Step -1
        If Not IsError(Application.Match(.Range("L" & i).Value, Array("Closed By Bank", "With Requestor For Closure", "Abandoned by Requestor"), 0)) Then
            .Rows(i).Delete
        End If
    Next i
End With
Application.ScreenUpdating = True

End Sub