+ Reply to Thread
Results 1 to 14 of 14

Search for occurences of text string from a list

  1. #1
    Registered User
    Join Date
    11-16-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Search for occurences of text string from a list

    Hello

    I am trying to come up with a formula that will allow me to check whether a particular text string exists in a cell and if it does write that value in the adjacent cells.

    To explain, I have a column containing about 50k text phrases. I have another column on a separate worksheet containing about 7k place names. I want to check every cell in the first column to see if there are any occurences of any of the place names from the second list. If there is a match then the value from the second list should be written to the adjacent cell in the first list. I have attached a sample workbook to clarify.

    The lookup needs to be case insensitive but should only match whole words (ie london should match with London but chester should not match with Manchester). Also some of the place names in the second list are multiple words.

    Any assistance greatly appreciated

    Howard
    Attached Files Attached Files
    Last edited by howardphillis; 05-25-2012 at 04:18 AM. Reason: Extra details

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Search for occurences of text string from a list

    In B2 on your phrases sheet you could use:

    =INDEX(Locations!A:A,MAX(INDEX(ROW(Locations!$A$2:$A$12)*(ISNUMBER(FIND(UPPER(Locations!$A$2:$A$12),UPPER(A2)))),0)))

    But I think this will fail by the time you get to 7,000 rows of locations, so you'd probably need a UDF (User Defined Function) written in Excel.

    Thing is, how do you want to handle multiple matches? For example, if you have the phrase "How do I get out of Basingstoke?" then in the locations it would match against both "Basingstoke" and "Stoke"

  3. #3
    Registered User
    Join Date
    11-16-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Search for occurences of text string from a list

    Hi Andrew

    thanks for the help. In the scenario you suggest I would want the result to return "Basingstoke" and not "Stoke"

    I will try the formula you suggest and see what happens

    Howard

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Search for occurences of text string from a list

    The formula will work for your sample file, but ISTR that there is a limit to how many elements you can have in an array formula, and I'm pretty sure that 7,000 is over that limit.

    The other question about multiple matches is how you deal with strings such as, "Nightlife in Newcastle". Will your locations list have an entry just for "Newcastle" or will it have entries for "Newcastle upon Tyne" and "Newcastle under Lyme"?

  5. #5
    Registered User
    Join Date
    11-16-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Search for occurences of text string from a list

    Just tried it and it almost works, but it doesn't check for whole words, so for example "Bathing costumes in Exeter" returns "Bath"

    Any other thoughts?

    Howard

  6. #6
    Registered User
    Join Date
    11-16-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Search for occurences of text string from a list

    Quote Originally Posted by Andrew-R View Post
    The formula will work for your sample file, but ISTR that there is a limit to how many elements you can have in an array formula, and I'm pretty sure that 7,000 is over that limit.

    The other question about multiple matches is how you deal with strings such as, "Nightlife in Newcastle". Will your locations list have an entry just for "Newcastle" or will it have entries for "Newcastle upon Tyne" and "Newcastle under Lyme"?
    Currently my list has "Newcastle upon Tyne" and "Newcastle under Lyme" as separate entries, and so woul not return any value for "Nightlife in Newcastle". I think the only realsitic workaround for this scenario is adding a value for Newcastle and then manually cleaning the data. But I am happy with that as an option.

  7. #7
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Search for occurences of text string from a list

    Hi

    May i suggest this?

    Please Login or Register  to view this content.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  8. #8
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Search for occurences of text string from a list

    I'm not sure if Fotis' suggestion will work with your full data set, or if it will run into the same limitations as my formula.

    Anyway, the following UDF should do the job:

    Please Login or Register  to view this content.
    With this in a module in your workbook you can use a formula such as:

    =listmatch(A2,Locations!$A$2:$A$12)

  9. #9
    Registered User
    Join Date
    11-16-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Search for occurences of text string from a list

    Hi Andrew

    thanks for the code, but unfortunately we are still getting the problem of it not matching entire words, so that "bathing suits in exeter" matches "Bath".

    I was thinking of adding a space before and after each place name - not very elegant I know, but would it work?

    Howard

  10. #10
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Search for occurences of text string from a list

    Sorry, Howard, that was my mistake. Change the line:

    Please Login or Register  to view this content.
    To:

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    11-16-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Search for occurences of text string from a list

    That is awesome! Thank you so much for your help

    Howard

  12. #12
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Search for occurences of text string from a list

    Happy to help, but I'm not sure how fast it will be when it's used 50k times against 7k rows of data - you might want to get yourself a good book to read

    A better idea would be to write a macro to go through your 50k rows, run my function against them, and write the return value into the appropriate cell. It won't be any quicker, but at least it won't constantly re-calculate and you can, instead, just run it when you're going on lunch, or at 5pm on a Friday, or just before you start a 6 month world-cruise, etc.

  13. #13
    Registered User
    Join Date
    11-16-2009
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Search for occurences of text string from a list

    Tried it on 10k rows and it only took about 20 seconds, and that was checking against 3 different lists of locations, product types and brands.

  14. #14
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Search for occurences of text string from a list

    The number of locations will make a big difference, but try it and see how it goes. If need be I can write a quicker function.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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