# Macro to Compare Two Columns in Excel and Highlight Differences

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.

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

In this section, you will learn 2 methods of 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
Â Â Â Â Â MsgBox "Please Select Two Columns"
Â Â Â Â Â 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``````

Your code is now ready to run.

• 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, and you will notice that there is a new dialog box asking for the location of the two columns.
• Then select the 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.

### 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 column 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 dialog box like the below one will appear.
• Therefore, select HighlightMissingData and press Run.

• After that press Run, and you will notice that there is a new dialog box asking for the location of the two columns.
• Then select the range of cell B5:C15 and press OK.

• Finally, you will see that the mismatched data between two columns is now present in the range of cells C16:C18.

You can download the free practice Excel template from here.

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

<< Go Back to Columns | Compare | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Advanced Excel Exercises with Solutions PDF