Excel - Compare data between different columns

July 2017



Issue


I need a macro to compare data between different columns

a(reference data) b(raw data) c(status) 

ab xc 
bc cb 
cd ab match 
ef pq 
gh cd match 
ij zx 
kl qr 
hj 
rh 
bc match 
kl match 

Solution


Try this:

Try the following code:
Sub test()
Dim lRow, lRow2 As Integer
lRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
lRow2 = Sheets("Sheet2").Range("D" & Rows.Count).End(xlUp).Row

For Each cell In Sheets("Sheet1").Range("A1:A" & lRow)
    With Sheets("Sheet2").Range("D1:D" & lRow2)
        Set c = .Find(cell, LookIn:=xlValues)
        If Not c Is Nothing Then
        firstAddress = c.Address
            Do
                Sheets("Sheet3").Cells(c.Row, "A") = "Match"
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
Next cell

End Sub


Thanks to TrowaD for this tip.

Related


Published by aakai1056. Latest update on February 2, 2012 at 08:37 AM by aakai1056.
This document, titled "Excel - Compare data between different columns," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).