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.
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:
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.
- How to compare two columns and highlight the unmatched data in Excel?
- How to exactly compare two strings (case sensitive) in Excel?