+ Reply to Thread
Results 1 to 8 of 8

Compare words between two cells for percentage match

  1. #1
    Registered User
    Join Date
    09-13-2010
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    2

    Compare words between two cells for percentage match

    Hi folks,

    I have a question that is similar but different to an earlier post (April 26 to be precise - same title). I am a relative beginner with VB so it would be fantastic if someone could lend me a hand.

    I have two columns of text each with 1,000+ lines. I want a formula that generates a percentage match for the words in cells of the same row (eg cells B1 and C1).

    I have a formula (courtesy of the earlier post) that counts the number of differing characters in the two cells (see below for VBA code). What I really want is a formula that counts the number of differing words.

    Example:
    Cell A1: the cat sat on the mat
    Cell B1: the hat was squashed by the cat

    The Levenshtein formula (code and link below) gives me an output of 14 ie, there are 14 different characters in cell B1 than in cell A1. The result I am looking for is that there are 4 words used in cell B1 that are not used in cell A1.

    If there is a way to measure the change in the order of words I would be interested in that also, but I realise that is probably very complicated.

    Any help is much appreciated!

    The formula I have was posted on this forum by SpeedingLunatic in April 2010 and is a Levenshtein measure. The VBA was borrowed from here: http://en.wikibooks.org/wiki/Algorit...htein_distance and is below...

    Please Login or Register  to view this content.
    Last edited by pike; 09-13-2010 at 02:27 AM. Reason: code tags for newbie MP message

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Compare words between two cells for percentage match

    You could use this function:
    Please Login or Register  to view this content.
    Regards,
    Antonio
    Last edited by antoka05; 09-13-2010 at 02:47 AM.

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Compare words between two cells for percentage match

    Below is similar but has case-sensitivity.

    Please Login or Register  to view this content.
    Regards change in order - you might need to specify some examples so we can better interpret requirements.

    Note: regards both UDFs presented - both assume a consistent delimiter between words (ie no punctuation per se) - if this is not reality you will need to clean the strings before comparing.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Compare words between two cells for percentage match

    Please Login or Register  to view this content.
    if you also want to take the words' order into account :
    Please Login or Register  to view this content.
    In this last case you can choose to weigh the different matches: a return of 4 consecutive words can be multiplied by 4, a match of 3 consecutive words can be multiplied by 3.
    It's also possible to count the different kind of matches in separate variables or array-elements.
    Last edited by snb; 09-13-2010 at 04:10 AM.



  5. #5
    Registered User
    Join Date
    09-13-2010
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Compare words between two cells for percentage match

    [SOLVED} Thanks very much to the three of you for you suggestions, and apologies that it has taken me a couple of days to get back to the forum.

    The code you supplied is perfect for my needs.

    Cheers!

  6. #6
    Registered User
    Join Date
    09-22-2010
    Location
    london,england
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Compare words between two cells for percentage match

    how do i use the vb script that snb posted above ? how do i insert it and where

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Compare words between two cells for percentage match

    You can put the code in the sheet-module in which you want to count the words.
    If you put them in another module (macro/workbook/userform module) you'll have to add the sheetname the cells are in.
    NB. the preceding period: .Cells(...
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-22-2010
    Location
    london,england
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Compare words between two cells for percentage match

    i've run your macro and for some reason i don't get any output.

    the steps to runing the macro were: (using 2007 version)
    alt+f11, paste in module 1 the macro,in excel i go went to view macro, select and run macro

    the selected cell stays blank after running the macro (any of them)


    any ideea why ?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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