Excel VBA检查值是否不在

时间:2016-12-02 02:34:23

标签: excel vba excel-vba

我有两个应该相同的范围(尽管它们的排序可能不同)。我试图在rangeA中找到不在rangeB中的任何值。

我能够找到一些示例,显示某个范围内的值是否匹配,但是如果不匹配则难以找到任何内容。

到目前为止,我有:

Sub Compare2()

    Dim test1, cell As Range
    Dim FoundRange As Range

    Set test1 = Sheets("Names").Range("A1:A5")
    For Each cell In test1

        Set FoundRange = Sheets("Queue & Status").Range("A1:A200").Find(what:=test1, LookIn:=xlFormulas, lookat:=xlWhole)

        If FoundRange Is Nothing Then
            MsgBox (cell & " not found")    
        End If

    Next cell
End Sub

但它显示所有值都不匹配,当它们存在时。

2 个答案:

答案 0 :(得分:2)

试试这个

Sub Compare2()

    Dim test1 As Range
    Dim lookIn As Range
    Dim c As Range
    Dim FoundRange As Range

    Set test1 = Sheets("Names").Range("A1:A5")
    Set lookIn = Sheets("Queue & Status").Range("A1:A200")
    For Each c In test1

        Set FoundRange = lookIn.Find(what:=c.Value, lookIn:=xlFormulas, lookat:=xlWhole)

        If FoundRange Is Nothing Then
            MsgBox (c.Value & " not found")
        End If

    Next c
End Sub

答案 1 :(得分:0)

what:= test1应该是:= cell(甚至是:= cell.value)