Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to compare two strings for similarity or highlight differences in Excel?

In some cases, you may only need to compare two adjacent cells of strings and mark their similarities or differences in Excel. This article provides two methods for you to achieve it.

Compare two strings with formula

Compare two strings for similarity or highlight differences with VBA code


Easily compare two ranges/worksheets and highlight the same/different cells in Excel

Click Kutools > Select > Select Same & Different Cells. The Kutools for Excel's Select Same & Diffferent Cells utility helps you easily compare two ranges or worksheets and then highlight the same or different cells in Excel. See below screenshot:

Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 60 days. Download the free trial now

Excel Productivity Tools

Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial

Kutools for Excel: Save 70% of your time and solve 80% Excel problems for you. 300+ advanced features designed for 1500+ work scenario, make Excel much easy and increase productivity immediately. 60-day Unlimited Free Trial


Compare two strings with formula


As below screenshot shown, if you just want to know whether the compared strings are matched or not, you can apply the following formula.

1. Select a blank cell C2, enter formula =EXACT(A2, B2) into the Formula Bar, and then press the Enter key. See screenshot:

Note: In the formula, A2 and B2 are the cells containing the comparing strings.

2. Keep selecting the result cell, then drag the Fill Handle to the cells until getting all compared results.

The FALSE result means that the compared strings are different, and the TRUE result indicates the two compared strings are matched. See screenshot:


Compare two strings for similarity or highlight differences with VBA code

If you want to compare two strings and highlight the similarities or differences between them. The following VBA code can help you.

1. Press Alt + F11 keys simultaneously to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, click Insert > Module. Then copy and paste the following code into the Code window.

VBA code: Compare two column strings for similarity or highlight differences

Sub highlight()
    Dim xRg1 As Range
    Dim xRg2 As Range
    Dim xTxt As String
    Dim xCell1 As Range
    Dim xCell2 As Range
    Dim I As Long
    Dim J As Integer
    Dim xLen As Integer
    Dim xDiffs As Boolean
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
lOne:
    Set xRg1 = Application.InputBox("Range A:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg1 Is Nothing Then Exit Sub
    If xRg1.Columns.Count > 1 Or xRg1.Areas.Count > 1 Then
        MsgBox "Multiple ranges or columns have been selected ", vbInformation, "Kutools for Excel"
        GoTo lOne
    End If
lTwo:
    Set xRg2 = Application.InputBox("Range B:", "Kutools for Excel", "", , , , , 8)
    If xRg2 Is Nothing Then Exit Sub
    If xRg2.Columns.Count > 1 Or xRg2.Areas.Count > 1 Then
        MsgBox "Multiple ranges or columns have been selected ", vbInformation, "Kutools for Excel"
        GoTo lTwo
    End If
    If xRg1.CountLarge <> xRg2.CountLarge Then
       MsgBox "Two selected ranges must have the same numbers of cells ", vbInformation, "Kutools for Excel"
       GoTo lTwo
    End If
    xDiffs = (MsgBox("Click Yes to highlight similarities, click No to highlight differences ", vbYesNo + vbQuestion, "Kutools for Excel") = vbNo)
    Application.ScreenUpdating = False
    xRg2.Font.ColorIndex = xlAutomatic
    For I = 1 To xRg1.Count
        Set xCell1 = xRg1.Cells(I)
        Set xCell2 = xRg2.Cells(I)
        If xCell1.Value2 = xCell2.Value2 Then
            If Not xDiffs Then xCell2.Font.Color = vbRed
        Else
            xLen = Len(xCell1.Value2)
            For J = 1 To xLen
                If Not xCell1.Characters(J, 1).Text = xCell2.Characters(J, 1).Text Then Exit For
            Next J
            If Not xDiffs Then
                If J <= Len(xCell2.Value2) And J > 1 Then
                    xCell2.Characters(1, J - 1).Font.Color = vbRed
                End If
            Else
                If J <= Len(xCell2.Value2) Then
                    xCell2.Characters(J, Len(xCell2.Value2) - J + 1).Font.Color = vbRed
                End If
            End If
        End If
    Next
    Application.ScreenUpdating = True
End Sub

3. Press the F5 key to run the code. In the first Kutools for Excel dialog box, select the first column of text strings you need to compare, and then click the OK button.

4. Then the second Kutools for Excel dialog box pops up, please select the second column strings, and click the OK button.

5. In the last Kutools for Excel dialog box, if you want to compare strings for similarity, click the Yes button. And to highlight the differences of the compared strings, click the No button. See screenshot:

Then you can see the compared results as below screenshot shown.


Related articles:


Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  • Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  • Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  • Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  • Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  • No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  • Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  • 60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  • Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Stefan · 5 months ago
    Very useful script! thanks
  • To post as a guest, your comment is unpublished.
    Eddy · 7 months ago
    Hello
    How do I copy Column1 next to Column2 if one or more items in Column 1 resembles / is identical to one or more items in Column2?
    I'm sorry but I've been working with Excel VBA for a while and I can not find the answer to this question.
    Thank you in advance for your answer.
  • To post as a guest, your comment is unpublished.
    Eddy · 7 months ago
    Hello
    How do I copy Column1 next to Column2 if one or more items in Column 1 resembles / is identical to one or more items in Column2?
    I'm sorry but I've been working with Excel VBA for a while and I can not find the answer to this question.
    Thank you in advance for your answer.
  • To post as a guest, your comment is unpublished.
    Carlos · 1 years ago
    Need to know how I can identify strings with the same text format so I can link an acct to all those strings. For instance, if I have 1,000 cells with different content, I want to separate those that have format 042-XXX-XX-00 to link them to an acct#.
  • To post as a guest, your comment is unpublished.
    SDJ · 1 years ago
    Thank you!