+ Reply to Thread
Results 1 to 5 of 5

Extract 1st matching substring from cell

  1. #1
    Registered User
    Join Date
    11-10-2008
    Location
    Michigan
    Posts
    2

    Extract 1st matching substring from cell

    I am trying to make a simple formula to populate a cell with the first matching substring found in a data cell (from a fixed list of substring keys).

    Example of desired results:

    Substring data: {X, YY, ZZ} (typically 6 to 15 values in real world use)

    Data..........Calculated result..........Comment
    AAX.................X........................Substring X was found in Data.

    ABC.............[empty]...................None of substrings were (X, YY, ZZ were found)

    XYYZZ.............X......................(only first match need be returned)

    ABCZZ.............ZZ....................Substring ZZ found etc….



    I am looking for something cleaner than nested if statements since there are likely to be a lot of values and different variations. The work would then be to copy the formula and populate the list of substrings to search for in the data.

    I tried some array formulas using search() but no luck so far.

    Thanks,
    Mike
    Last edited by VBA Noob; 11-26-2008 at 03:35 PM. Reason: Formatting edit

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Try (assuming your first string is in A2):

    Please Login or Register  to view this content.
    Which must be confirmed with CTRL+SHIFT+ENTER not just ENTER... you will see { } brackets appear.. then copy down..

    You can replace the substring: {"X","YY","ZZ"} with a range containing those values (without quotes).

    Any changes to the formula require reconfirmation using CSE keys.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996
    With
    A1:A4 containing this list:
    AAX
    ABC
    XYYZZ
    ABCZZ

    and....F1:F3 containing this list
    X
    YY
    ZZ

    Try this...
    Please Login or Register  to view this content.
    Copy that formula down through B4.

    In the above example, the formulas return these values:
    Please Login or Register  to view this content.
    Is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  4. #4
    Forum Expert
    Join Date
    06-18-2004
    Location
    Canada
    MS-Off Ver
    Office 2016
    Posts
    1,474
    Try the following formula, which needs to be confirmed with CONTROL+SHIFT+ENTER...

    =LOOKUP(REPT("z",255),CHOOSE({1,2},"",INDEX({"X", "YY", "ZZ"},MATCH(TRUE,ISNUMBER(FIND({"X","YY","ZZ"},A2)),0))))

    or

    =LOOKUP(REPT("z",255),CHOOSE({1,2},"",INDEX($G$2:$G$4,MATCH(TRUE,ISNUMBER(FIND($G$2:$G$4,A2)),0))))

    ...where G2:G4 contain X, YY, and ZZ. Note that FIND is case-sensitive. If you don't want the formula to be case-sensitive, replace FIND with SEARCH.

    Hope this helps!

  5. #5
    Registered User
    Join Date
    11-10-2008
    Location
    Michigan
    Posts
    2
    Thanks (!) All 3 examples worked perfectly.

+ 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