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.

dataset of macro to compare two columns in excel 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:

  • Press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

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

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

  • Press F5 on your keyboard or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the small Play icon in the sub-menu bar to run the macro.

  • A pop-up box will appear, asking you to Select Two Columns. Select the column range that you want to compare (in our case, it was B5:C15) and click OK.

result of macro to compare two columns in excel and highlight differences

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.

dataset of macro to compare two columns in excel and highlight differences of missing data

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

Your code is now ready to run.

  • Run the macro and a pop-up box will appear, asking you to Select Two Columns. Select the column range that you want to compare (in our case, it was B5:C15) and click OK.

You will get the missing data from your two compared columns placed under the column of missing values.

result of macro to compare two columns in excel and highlight differences of missing data

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

ExcelDemy
Logo