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

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.

## 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
`````` 