1

I have two ranges that should be identical (albeit they may be sorted differently). I'm trying to find any values in rangeA that are not in rangeB.

I'm able to find examples that show if values ARE matched in a range, but struggling to find anything if they aren't.

So far I have:

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

But it's showing all values as being not matched, when they are.

4
  • Do you really need done it with vba? Commented Dec 2, 2016 at 2:44
  • try changing test1 to cell in the Set FoundRange line. Set FoundRange = Sheets("Queue & Status").Range("A1:A200").Find(what:=cell, LookIn:=xlFormulas, lookat:=xlWhole) Tested and seems to work ok. Commented Dec 2, 2016 at 2:47
  • FYI Dim test1, cell As Range is declaring cell as a Range, and test1 as an implicit Variant. Commented Dec 2, 2016 at 2:51
  • 1
    Are you really wanting to LookIn formulas, or are you trying to compare values? (It won't matter which you do if all the cells are constants, but could cause problems if any of the data is calculated.) Commented Dec 2, 2016 at 3:17

2 Answers 2

3

Try this

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
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, i noticed that too, but it worked so i posted anyways
0

what:=test1 should be what:=cell (or even what:=cell.value)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.