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

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

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:


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
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 · 1 years ago
    Very useful script! thanks
  • To post as a guest, your comment is unpublished.
    Eddy · 1 years 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 · 1 years 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 · 2 years ago
    Thank you!
  • To post as a guest, your comment is unpublished.
    Yogesh · 2 years ago
    Above code dose not works if any cell contains formula.
  • To post as a guest, your comment is unpublished.
    suryateja · 2 years ago
    Dear sir,
    Thank you very much for your VBA code.

    It is very much helpful.

    Actually i dont have any knowledge about coding.
    So, even to modify a little part of coding according to my requirement, i am helpless

    I have one request about this coding to make it more efficient and user friendly.
    Could you please help me??

    The main objective of above coding is to find and highlight the differences between the data in two columns for easy reference.
    But, this coding is little complicated as objective is concerned.

    Becuase, it is considering the "spaces" between the content and it is highlight the entire data even though the strings are same.
    So, if we can compare the data without spaces, then we can filter the portion of highlighted data.
    Also, instead of highlighting only the strings those which are not similar, it is hightlighting entire data right from the first different string to the data till the end of the cell.

    Hence, i would like to conclude my request as below 2 points.
    1) please edit the coding to compare the cells without consideirng the spaces
    2) please edit the coding in such a way that it can highlight only different strings but not highlighting entire para from the first different string point.

    Thank you very much for you help.

    Regards,
    Surya
    • To post as a guest, your comment is unpublished.
      Salai · 2 years ago
      i have the same request as suryateja.
      2) please edit the coding in such a way that it can highlight only different strings but not highlighting entire para from the first different string point.