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.


Download Practice Template

You can download the free practice Excel template from here.


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.

VBA Macro to Compare Two Columns and Highlight Differences

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.

Initialization of the VBA through Visual Basic from Developer tab

  • Then in the pop-up code window, from the menu bar, click Insert -> Module.

Inserting the editor Module inside the VBA editor

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

Opening Macros through View tab

  • Then a dialog box like the below one will appear.
  • Therefore, select HighlightColumnDfferences and press Run.

Choosing appropriate macro from the Macro dialogue box

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

Select the columns that needs to be compared

  • After pressing OK, you will notice that the cells that are different in the columns in the range of cell B5:C15

THe mismatched values in the rows now highlighted in blue color.

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


Similar Readings


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.

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

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

Run VBA Macro from View tab

  • Then a dialogbox like the below one will appear.
  • Therefore, select HighlightMissingData and press Run.

choose apprpriate macro from Macro dialogue box

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

range of the columns are now chosen

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

all the missing data are now showing 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

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

Leave a reply

5 Excel Hacks You Never Knew

Genius tips to help you unlock Excel's hidden features

FREE EMAIL BONUS

ExcelDemy
Logo