+ Reply to Thread
Results 1 to 5 of 5

Compare Text In One Cell To Another

  1. #1
    Registered User
    Join Date
    07-18-2024
    Location
    United States
    MS-Off Ver
    MS 365
    Posts
    33

    Compare Text In One Cell To Another

    I am trying to develop a program where the end state will be this program finding all instances of similar text within cells from multiple sheets and grouping it together. This question is going to deal specifically with the sub process I am trying to make dealing with the comparison portion. I created the following code which is simply a test to see how my comparison could work. It compares two cells right next to each other and goes letter by letter and spits out a similarity score. The code is below

    Please Login or Register  to view this content.
    The problem with this code is the results are not what I am wanting. For example if I have entries "This is a test string" compared to "Ths is a text string" due to my macro comparing value by value a mistake in the beginning makes the similarity score very low when in actuality the only error is a missing "i" Does anyone have suggestions for this code to compare word by word instead of value by value? I use the word value here because there will be instances where the "text" will include numbers such as 90% for example.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Compare Text In One Cell To Another

    One industry standard for this is the Levenshtein score. The higher the score, the more different they are. I don't think there's an upper limit; it depends on the text you are comparing. Redesign your code to disentangle the part that actually does the comparison and use the below:

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,073

    Re: Compare Text In One Cell To Another

    I've used this function with some success in the past as a UDF. It scores the similarity as a percentage where 100 = identical.
    Depending on the data, you can usually establish a threshold percentage over which the strings are similar enough to warrant further consideration.

    Please Login or Register  to view this content.
    This is a test string VS Ths is a text string scores 74%

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Compare Text In One Cell To Another

    The Levenshtein Distance is not a fixed scale, so won't give you a percentage. However, you can divide the LD by the length of the longer string, then subtract from 1, to get a percentage to tell you how similar they are.

    This is a test string
    Ths is a text string
    LD: 2
    Score: 90%

    This is a test string
    This is a test string
    LD: 0
    Score: 100%

    This is a test string
    Ceci n'est pas une pipe
    LD: 19
    Score: 17%

    This is a test string
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    LD: 35
    Score: 0%

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Compare Text In One Cell To Another

    To wit:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Compare a text cell with a numeric cell and output a text string
    By ids in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-25-2014, 07:39 AM
  2. [SOLVED] Compare text in a cell to text in a column on another sheet & return text from 3rd cell
    By marks@bcmr.com in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-06-2014, 05:33 AM
  3. [SOLVED] Compare multi-line text cell to data in much larger text file
    By Rhudi in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-30-2013, 05:19 PM
  4. Replies: 6
    Last Post: 02-28-2013, 11:27 AM
  5. Replies: 2
    Last Post: 01-30-2013, 12:59 PM
  6. Compare text in a cell with the text in the previous cell and return a value
    By kinley in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-09-2010, 04:14 AM
  7. Compare cell text to set list and show text differences
    By NSTurk725 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 12-06-2009, 09:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1