How to compare two columns in Excel to find duplicates

January 4, 2012| Posted by : | Filled in Excel Functions, Excel Macro

Many a times we require to compare two columns data and find out all the Duplicates. So in this article you are going to see how we can compare two columns in Excel and find out duplicates.

There are two ways of comparing two columns in an Excel Worksheet.

1.By Using Excel Formula 2. By Using Excel Macro

Compare two columns by using Excel Formulas:

By using ISERROR and MATCH formula we can compare 2 columns.
Type the below formula in the column where you want the list of all duplicates in both the column

=IF(ISERROR(MATCH(A1,$C$1:$C$5,0)),"",A1) 

Where:
A1 - Is the column Which you want to be compared
$C$1:$C$5 - Is the Range which you want compared with

Compare Two Column in Excel Formula

Compare Two Column in Excel Formula

Note: In the above formula, whichever cell it is finding as duplicate in Column A, it will populate in the same row. As shown in the above image.

Compare two columns by using Excel Macro:

1. This function will compare Column A with Column B and list the Duplicates in Column C in a Sequence.


Private Sub CommandButton1_Click()
Dim CompareRange As Variant, To_Be_Compared As Variant, x As Variant, y As Variant

Range("A1").Select
Selection.End(xlDown).Select
Set To_Be_Compared = Range("A1:" & Selection.Address)
Range("B1").Select
Selection.End(xlDown).Select
Set CompareRange = Range("B1:" & Selection.Address)

'If you want to exchange the columns like if you want to
'compare B  with A then chnage the range
'and selection or vise-versa

i = 1
To_Be_Compared.Select

    For Each x In Selection
        For Each y In CompareRange
            If x = y Then
              Range("C" & i).Value = x
              i = i + 1
            End If
        Next y
    Next x
End Sub

2. This function will compare Column A with Column B and list the Duplicates in Column C but not in a Sequence. It will list exactly in the same row which is duplicate.

Private Sub CommandButton2_Click()
Dim CompareRange As Variant, To_Be_Compared As Variant, x As Variant, y As Variant

Range("A1").Select
Selection.End(xlDown).Select
Set To_Be_Compared = Range("A1:" & Selection.Address)
Range("B1").Select
Selection.End(xlDown).Select
Set CompareRange = Range("B1:" & Selection.Address)

'If you want to exchange the columns like if you want to
'compare B  with A then chnage the range
'and selection or vise-versa

To_Be_Compared.Select

    For Each x In Selection
        For Each y In CompareRange
            If x = y Then x.Offset(0, 2) = x
        Next y
    Next x
End Sub

3. This function will ask for Column to Compare, Column To be compared and Column to list the Duplicates. This is completely dynamic. It can complare any Column with any Column and list all the duplicates in any of the column.


Private Sub CommandButton1_Click()
Dim CompareRange As Variant, To_Be_Compared As Variant, x As Variant, y As Variant
str1 = InputBox("Enter Column Name to be Compared")
str2 = InputBox("Enter Column Name to Compare")
str3 = InputBox("Enter Column Name to put the Result")
Range(str1 & "1").Select
Selection.End(xlDown).Select
Set To_Be_Compared = Range(str1 & "1:" & Selection.Address)
Range(str2 & "1").Select
Selection.End(xlDown).Select
Set CompareRange = Range(str2 & "1:" & Selection.Address)

'If you want to exchange the columns like if you want to
'compare B  with A then chnage the range
'and selection or vise-versa

i = 1
To_Be_Compared.Select

    For Each x In Selection
        For Each y In CompareRange
            If x = y Then
              Range(str3 & i).Value = x
              i = i + 1
            End If
        Next y
    Next x
End Sub

No related posts.


Subscribe To Get New Posts delivered directly to your Mailbox

11 Responses to “How to compare two columns in Excel to find duplicates”

  1. Jack says:

    Hi,

    I was trying to do this in Macro for so long. By using this i am able to save lots of time now.

    Thanks alot.

  2. mona says:

    Hi,

    It was really helpful, can you please shed some light on a situation where we want to Highlight duplicates if col A and B are the same but not col C.

    Your help is highly appreciated :)

    Thanks,

    Mona

  3. nur says:

    Dear Vish Bhaia, i want to get work book. please provide me…

  4. My problem is similar, but is about two comblist. my data base is A1:F1900 from this a:f I need to calculate all the gaps of each row first, and after generate a list with the same gaps, so the new list that I have in another sheet has to be compare and remove duplicates, and second from the a:f data need to calculate the differences by columns generate a list with this differences compare if this new list don't have the same interval with a1:f6 and again remove the duplicates from the other sheet. if somebody give me a hand in this, would be nice, is a real challenge.

  5. satish says:

    Excel-Sheet1 Excel-Sheet2 Result-in-Sheet3

    Name Place Name Place Name Place

    —————— ———————- —————-

    satish mysore satish mysore TRUE TRUE

    ravi bellary ravi Bangalore TRUE FALSE

    Reddy pennukonda venu pennukonda FALSE TRUE

    Need macros to compare sheet-1 with Sheet-2 and results as True or False.

    It should compare Sheet-1 with Sheet-2 from 2nd row to the End of the excel. Please provide me the working Macros. Thanks in advance.

  6. satish says:

    Excel-Sheet1

    Name Place

    ————–

    satish mysore

    ravi bellary

    Reddy pennukonda

    Excel-Sheet2

    Name Place

    ————–

    satish mysore

    ravi Bangalore

    venu pennukonda

    Need to compare the sheet-1 and sheet-2 and to get the result in 3rd Excel sheet-3 as follows. Please provide the Excel Macros for this comparision. It should compare the result from 2nd row to end of the excel sheet where ever the data is present.

    Result-Sheet3

    Name Place

    ————-

    TRUE TRUE

    TRUE FALSE

    FALSE TRUE

Leave a Reply

Comments

comments

  • About
  • Downloads
  • Excel Formula
  • Excel Tips
  • Excel Macro Tutorial
  • VBA Functions
  • Archives
  • Contact
  • Excel Formula
  • Excel Functions
  • Excel Macro
  • Excel Macro Basics
  • Excel Macro Tutorial
  • Excel Tips
  • HP QC
  • Interesting VBA Functions
  • New to Excel Macro ?
  • Personal
  • Popular Articles
  • 2014
  • 2013
  • 2012
  • 2011