# Macro to Compare Two Columns in Excel and Highlight Differences

Get FREE Advanced Excel Exercises with Solutions!

Sometimes we need to highlight some data in Excel to have a better understanding of our dataset. Implementing VBA macro is the most effective, quickest and safest method to run any operation in Excel. In this article, we will show you how to implement macro to compare two columns in Excel and highlight differences among them.

## 2 Methods in Implementing VBA Macro to Compare Two Columns and Highlight Differences in Excel

In this section, you will learn 2 methods in implementing the VBA macro to compare two columns and highlight differences and the missing data among them in Excel.

### 1. VBA Macro to Compare Two Columns and Highlight Differences in Excel

Look at the following dataset consisting of two columns, Fruit List 1 and Fruit List 2. We will compare these two columns and find out the differences among them using VBA. Steps to compare columns Fruit List 1 and Fruit List 2 and highlight differences among them with VBA are given below.

Steps:

• At first, press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor. • Then in the pop-up code window, from the menu bar, click Insert -> Module. • After that copy the following code and paste it into the code window.
``````Sub HighlightColumnDifferences()
Dim Rg As Range
Dim Ws As Worksheet
Dim FI As Integer
On Error Resume Next
SRC:
Set Rg = Application.InputBox("Select Two Columns:", "Excel", , , , , , 8)
If Rg Is Nothing Then Exit Sub
If Rg.Columns.Count <> 2 Then
GoTo SRC
End If
Set Ws = Rg.Worksheet
For FI = 1 To Rg.Rows.Count
If Not StrComp(Rg.Cells(FI, 1), Rg.Cells(FI, 2), vbBinaryCompare) = 0 Then
Ws.Range(Rg.Cells(FI, 1), Rg.Cells(FI, 2)).Interior.ColorIndex = 8 'you can change the color index as you like.
End If
Next FI
End Sub``````

• Then go to View> Macros>View Macros. • Then a dialog box like the below one will appear.
• Therefore, select HighlightColumnDfferences and press Run. • After that press Run, you wlll notice that there is new dialog box asking for the location of the two columns.
• Then select range of cell B5:C15 and press OK. • After pressing OK, you will notice that the cells that are different in the columns in the range of cell B5:C15 Hence You will get highlighted rows carrying different values in two columns.

Related Content: How to Compare Two Columns and Return Common Values in Excel

### 2. VBA Macro to Compare Two Columns and Highlight Differences of Missing Data in Excel

Look at the following dataset consisting of two columns, Fruit List 1 and Fruit List 2 where columns Fruit List 2 has some missing data compared to column Fruit List 1. We will compare these two columns and find out the missing data from them using the VBA macro. • Steps to compare columns Fruit List 1 and Fruit List 2 and highlight differences of missing data among them with VBA are shown below.

Steps:

• Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
• In the code window, copy the following code and paste it.
``````Sub HighlightMissingData()
Dim Rg, RgC1, RgC2, FRg1, FRg2 As Range
Dim IntR, IntSR, IntER, IntSC, IntEC As Integer
Dim Ws As Worksheet
On Error Resume Next
SRC:
Set Rg = Application.InputBox("Select Two Columns:", "Excel", , , , , , 8)
If Rg Is Nothing Then Exit Sub
If Rg.Columns.Count <> 2 Then
MsgBox "Please Select Two Columns as a Range"
GoTo SRC
End If
Set Ws = Rg.Worksheet
IntSC = Rg.Column
IntEC = Rg.Columns.Count + IntSC - 1
IntSR = Rg.Row
IntER = Rg.Rows.Count + IntSR - 1
Set Rg = Rg.Columns
Set RgC1 = Ws.Range(Ws.Cells(IntSR, IntSC), Ws.Cells(IntER, IntSC))
Set RgC2 = Ws.Range(Ws.Cells(IntSR, IntEC), Ws.Cells(IntER, IntEC))
IntR = 1
For Each FRg In RgC1
If WorksheetFunction.CountIf(RgC2, FRg.Value) = 0 Then
Ws.Cells(IntER, IntEC).Offset(IntR) = FRg
IntR = IntR + 1
End If
Next
IntR = 1
For Each FRg In RgC2
If WorksheetFunction.CountIf(RgC1, FRg) = 0 Then
Ws.Cells(IntER, IntSC).Offset(IntR) = FRg
IntR = IntR + 1
End If
Next
End Sub``````
• Then go to View> Macros>View Macros. • Then a dialogbox like the below one will appear.
• Therefore, select HighlightMissingData and press Run. • After that press Run, you will notice that there is new dialog box asking for the location of the two columns.
• Then select range of cell B5:C15 and press OK. • Finally, you will see that the mismatched data between two columns are now present in the range of cells C16:C18. Related Content: How to Compare Two Columns in Excel For Finding Differences

## Conclusion

This article showed you how to compare two columns and highlight differences among them in Excel by implementing the VBA macro. I hope this article has been very beneficial to you. Feel free to ask any questions regarding the topic.

## Related Articles Sanjida Ahmed

Hello World! This is Sanjida, an Engineer who is passionate about researching real-world problems and inventing solutions that haven’t been discovered yet. Here, I try to deliver the results with explanations of Excel-related problems, where most of my interpretations will be provided to you in the form of Visual Basic for Applications (VBA) programming language. Being a programmer and a constant solution seeker, made me interested in assisting the world with top-notch innovations and evaluations of data analysis.

We will be happy to hear your thoughts Advanced Excel Exercises with Solutions PDF  