How to Compare Two Strings for Similarity in Excel (6 Methods)

Get FREE Advanced Excel Exercises with Solutions!

In this tutorial, I am going to show you 6 easy methods on how to compare two strings for similarity in Excel. You can use these methods even in large datasets to find out cells that have exactly or partially similar strings. Throughout this tutorial, you will also learn some important Excel tools and techniques that will be very useful in any Excel-related task.


How to Compare Two Strings for Similarity in Excel: 6 Methods

We have taken a concise dataset to explain the steps clearly. The dataset has approximately 7 rows and 2 columns. Initially, we are keeping all the cells in General format. For all the datasets, we have 2 unique columns which are Sales Person Full Name and First Name. Although we may vary the number of columns later on if that is needed.

excel compare two strings for similarity


1. Using Highlight Cells Rules Feature

In this first method, we will see how to compare two strings for similarity using the Highlight Cells Rules in Excel. Follow the steps below.

Steps:

  • First, select all the cells from B5 to C10.

selecting the cells to compare two strings for similarity in excel

  • Next, go to the Home tab and click on Conditional Formatting.
  • Here, go to Highlight Cells Rules and click on Duplicate Values.

highlight cells rules to compare two strings for similarity in excel

  • Now, simply click on OK.

  • As a result, this should highlight the values that are similar.


2. Applying New Rule Feature

We can also use the New Rule feature in Excel which gives us custom options to compare two strings for similarity. Let us see how to do that.

Steps:

  • To begin with, again select the cells from B5 to C10.

applying new rule feature to compare two strings for similarity in excel

  • Now, navigate to Conditional Formatting under the Home tab and click on New Rule.

  • Next, in the new window, select Format only unique or duplicate values and click on Format.

  • Then, select a color under the Fill tab and click OK in this window and also in the next window.

  • Consequently, this will highlight the values that are similar in the dataset.


3. Utilizing Equal Operator

The equal operator in Excel can evaluate whether a statement is TRUE or FALSE. We will use this symbol to quickly compare two strings for similarity. Below are the detailed steps.

Steps:

  • First, go to cell D5 and insert the following formula:
=B5=C5

using equal operator to compare two strings for similarity in excel

  • Now, press Enter and copy this formula to the other cells using Fill Handle.
  • Finally, this will give TRUE or FALSE values based on whether the values match or not.


4. Comparing Using EXACT Function

The EXACT function in Excel is very useful if we want to compare two text strings for similarity. For this, we just need to give the two strings as inputs to this function. Let us see the step-by-step process.

Steps:

  • To begin this method, double-click on cell D5 and insert the formula below:
=EXACT(B5,C5)

using exact function to compare two strings for similarity in excel

  • Next, press the Enter key and consequently, this will insert TRUE if the values are exactly similar.


5. Using SEARCH Function

The SEARCH function in Excel can find the position of one string inside another string. So, we should be able to use this function to compare two strings for similarity. Follow the steps below to do this.

Steps:

  • To start this method, navigate to cell D5 and type in the following formula:
=IFERROR(IF(SEARCH(C5,B5),"Similar"),"Not Similar")

using search function to compare two strings for similarity in excel

  • After that, press the Enter key or click on any blank cell.
  • Immediately, this will give you the result as similar or not for all the data.

🔎 How Does the Formula Work?

  • SEARCH(C5,B5): This portion gives the true value as 1.
  • IF(SEARCH(C5,B5),”Similar”): This part gives the result back as Similar.
  • IFERROR(IF(SEARCH(C5,B5),”Similar”),”Not Similar”): This also returns the final value as Similar.

6. Applying VBA Code

If you are familiar with VBA in Excel, you can easily compare two strings for similarity. Although we will write a little lengthy code, you can just copy this code into your own file. Let us see how to write VBA code for this.

Steps:

  • For this method, go to the Developer tab and select Visual Basic.

using VBA code to compare two strings for similarity in excel

  • Now, select Insert in the VBA window and click on Module.

  • Next, type in the formula below in the new window:
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:", "Select Range", 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, "Similar or Not"
        GoTo lOne
    End If
lTwo:
    Set xRg2 = Application.InputBox("Range B:", "Select Range", "", , , , , 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, "Similar or Not"
        GoTo lTwo
    End If
    If xRg1.CountLarge <> xRg2.CountLarge Then
       MsgBox "Two selected ranges must have the same numbers of cells ", vbInformation, "Similar or Not"
       GoTo lTwo
    End If
    xDiffs = (MsgBox("Click Yes to highlight similarities, click No to highlight differences ", vbYesNo + vbQuestion, "Similar or Not") = 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
  • Then, open the macro from the Developer tab by clicking on Macros.

  • Now, in the Macro window, select the Highlight macro and click Run.

  • After that, insert the first range in the Select Range window and click OK.

  • Next, select the second range and again click OK.

  • Here, press Yes to confirm.

  • As a result, the VBA code will highlight a similar value in cell C8.


Download Practice Workbook


Conclusion

I hope that you were able to apply the methods that I showed in this tutorial on how to compare two strings for similarity in excel. As you can see, there are quite a few ways to achieve this. So wisely choose the method that suits your situation best. If you get stuck in any of the steps, I recommend going through them a few times to clear up any confusion. If you have any queries, please let me know in the comments.


<< Go Back To Excel Compare Cells | Compare in Excel | Learn Excel

What is ExcelDemy?

ExcelDemy - Learn Excel & Get Excel Solutions Center provides online Excel training , Excel consultancy services , free Excel tutorials, free support , and free Excel Templates for Excel professionals and businesses. Feel free to contact us with your Excel problems.
Shakil Ahmed
Shakil Ahmed

My name’s Shakil. My articles are targeted to support you in enriching knowledge regarding different features related to Microsoft Excel. I am fond of literature, hope some knowledge from them will help me providing you some engaging articles even though some weary technical terms.

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo