Macro To Compare Two Columns in Excel Spreadsheet And Highlight Differences

While we provided solution for comparing two columns in excel spreadsheet and highlight the duplicates, we realized that requirement can be reverse of it as the title of this post suggests – compare and highlight different entries in two columns of same excel worksheet. Here is a step by step procedure to compare data in two columns of same worksheet in excel and highlight the different entries. This Macro compares column 2 with column 1 and highlight the different entries in column 2. It also updates the operation’s progress in status bar and helps user in estimating the time taken for comparison.

Creating Macro

First step is to create a Macro. For creating a macro, click on Tools in menu bar, select Macro and then Macros in sub menu as shown in Figure 1 below.

Figure 1

Clicking on Macros in Figure 1 will open the below window. Type the name of Macro as “Compare” or a name of your choice and click on Create button.

Clicking on Create button will launch the Visual Basic editor as below.

Figure 3

Copy below code and paste it in the editor.

Sub Compare()
' Compare Macro
' compare two different columns in the active worksheet
    Dim c1 As Integer, c2 As Integer
    Dim i As Long, k As Long, count As Long
    Dim lr1 As Long, lr2 As Long
    Dim cf1 As String, cf2 As String
    Dim diffB As Boolean
    Dim WS As Worksheet
    ' Set the worksheet name
    Set WS = Worksheets("Sheet1")
    Application.ScreenUpdating = False
    Application.StatusBar = "Creating the report..."
    Application.DisplayAlerts = True
    ' Setting columns 1 & 2 for comparison (A and B in excel)
    c1 = 1
    c2 = 2
    With WS.UsedRange
     lr1 = .Rows.count
    End With
    For i = 2 To lr1
        diffB = True
        For k = 2 To lr1
            cf1 = ""
            cf2 = ""
            On Error Resume Next
            cf1 = WS.Cells(i, c2).FormulaLocal
            cf2 = WS.Cells(k, c1).FormulaLocal
            On Error GoTo 0
            If cf1 = cf2 Then
               diffB = False
            Exit For
          End If
          Next k
          If diffB Then
                count = count + 1
                WS.Cells(i, c2).Interior.ColorIndex = 19
                WS.Cells(i, c2).Select
                Selection.Font.Bold = True
          End If
    Next i
Application.StatusBar = "Formatting the report..."
'Columns("A:IV").ColumnWidth = 10
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox count & " cells contain different values!", vbInformation, _
"Compare column " & c2 & " with " & c1
End Sub

Save this Macro by closing the editor.

Running Macro

Select active sheet (Sheet1 in this example) and run the Macro by clicking on Tools->Macro->Macros. Select the Macro “Compare” and click on “Run” button. Different entries will be highlighted in second column.

In the above Macro, it is assumed that first row is a header row. Also, worksheet names is set as “Sheet1″ (refer variable WS in above macro) and columns to be compared are considered to be “1″ and “2″ (refer variables c1 and c2). To change the worksheet names/column numbers or customize this Macro further, follow below steps.

Editing Macro

  • Go to Tools->Macro->Macros. Select the Macro “Compare” and click on “Edit” button.
  • This will launch the Visual Basic Editor.
  • Customize the code as per the requirements.
  • Save the Macro.

Hope you find this article useful, your suggestions and feedback are always welcome. Thank You.

  1. I tried using the scripts it works good except for once case.
    One Column contained 1 and other column contained 0 in the same row.
    The difference was not detected at all.
    Pls let me know why

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>