VBA to Compare Two Columns

dim4x4

Board Regular
Joined
May 8, 2002
Messages
108
Hello!

I tried to find a solution to my question in exiting threads, but none of them exactly do what I want. The closest to what I need is this thread: VBA to Compare Columns, but for some reason, the solution there does not work in my case.

I have two columns A and B with names. They have different number of entries, but both over a 1000. I want in column D to list all the names which are present in column A, but absent from column B. In column E, I want to list all the names which are present in column B, but absent from column A. The lists in columns A and B should remain as is.

Thank you!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I'm assuming data has Header in A1 and B1

VBA Code:
Sub Compare_ListMissing()

Dim n&, eRow1&, eRow2&
Dim NotFound As Boolean
Dim cell1 As Range, cell2 As Range
Dim rng1 As Range, rng2 As Range

Application.ScreenUpdating = False

' Get last row for each data on Sheet1 and Sheet2
eRow1 = Range("A1").End(xlDown).Row
eRow2 = Range("B1").End(xlDown).Row

' Define Range for each sheet
Set rng1 = Range("A2", "A" & eRow1)
Set rng2 = Range("B2", "B" & eRow2)

' Compare list in rng1 to rng2
n = 1
NotFound = True
For Each cell1 In rng1
    For Each cell2 In rng2
        If cell2 = cell1 Then
            NotFound = False
            Exit For
        End If
    Next
    If NotFound Then
        n = n + 1
        Range("D" & n) = cell1.Value2
    End If
    NotFound = True
Next

n = 1
NotFound = True
' Compare list in ws2 to ws1
For Each cell2 In rng2
    For Each cell1 In rng1
        If cell1 = cell2 Then
            NotFound = False
            Exit For
        End If
    Next
    If NotFound Then
        n = n + 1
        Range("E" & n) = cell2.Value2
    End If
    NotFound = True
Next

' Return cursor to cell A1 for each worksheet
Application.Goto Range("A1"), True
       
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,942
Messages
6,122,367
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top