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.
Read More: Excel formula to compare two columns and return a value
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.
Download Practice Template
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!