选择多个条件时如何删除行

时间:2017-05-11 21:36:36

标签: excel-vba vba excel

嗨我有下面的代码,当它从多个工作表中找到特定的单个条件时删除行我想要用多个条件修改代码

Sub DeleteRow_IMPLEMENTATION()

Dim Header As Range
Dim FoundCell As Range
Dim ws As Worksheet
Dim HeaderToFind As String
Dim ValueToFind As String

Application.ScreenUpdating = False
Application.DisplayStatusBar = False


HeaderToFind = "BankName"
ValueToFind = "abcd"

For Each ws In Worksheets

    Set Header = ws.Rows(1).Find(what:=HeaderToFind, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)

    If Not Header Is Nothing Then

        Set FoundCell = ws.Columns(Header.Column).Find(what:=ValueToFind, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)

        Do While Not FoundCell Is Nothing
            ws.Rows(FoundCell.Row).delete
            Set FoundCell = Nothing
            Set FoundCell = ws.Columns(Header.Column).Find(what:=ValueToFind, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
        Loop

    End If

Next ws
Application.ScreenUpdating = True
Application.DisplayStatusBar = True

End Sub

我尝试了类似下面的内容

HeaderToFind = "BankName"
ValueToFind = "abcd,xyz "

当我尝试不工作任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

尝试在数组中设置您要查找的值,而不是逗号分隔的字符串。然后foreach数组中的值:将ValueToFind设置为等于该值,并调用此代码

HeaderToFind = "BankName"

Dim  targetValues = New String { "abcd", "xyz" }
For Each targetVal In targetValues
    ValueToFind = targetVal

    For Each ws In Worksheets
        ....
    Next ws
Next targetVal

或者,您可以看看: Efficient way to delete entire row if cell doesn't contain '@'

并将条件设置为值数组