+ Reply to Thread
Results 1 to 17 of 17

Formula(s) to extract string that matches certain criteria from a text

  1. #1
    Registered User
    Join Date
    01-09-2014
    Location
    Jakarta
    MS-Off Ver
    Excel 2013
    Posts
    6

    Formula(s) to extract string that matches certain criteria from a text

    Hi everyone!

    I have the following problem:

    I have many different texts which vary widely in their length, structure and so on.
    In most of those texts, at a varying position, there is a number with the following criteria:
    - starts with "30"
    - length is 9 digits

    So for example I have the following texts:
    1. "TRSF I-BAK CR 1912/FTSZE/W3251 842000.00 Mka Poluia Sode : 307827482"
    2. "#307827439"
    3. ""NA 308729762/M0CP Czh hha"
    4. "ATALA Trans 0700000224499 5111892056353077"

    The formula I search for should have the number as its output.

    Currently the following formula in place (Texts being in Column B):

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    However, it's far away from a real solution, e.g. it does not incorporate the mentioned length of the string and therefore e.g. finds a match in Example 4 even though this is one of the cases where the number is not in the text.

    I know that Excel is probably not the best tool for such a task but I hope you guys can help me out
    Thanks a lot in advance!

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Formula(s) to extract string that matches certain criteria from a text

    splat a IF around it?

    If Len<9 then "not found"?
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Forum Expert dredwolf's Avatar
    Join Date
    10-27-2012
    Location
    Clearwater,Canada
    MS-Off Ver
    Excel 2007
    Posts
    2,649

    Re: Formula(s) to extract string that matches certain criteria from a text

    The reason you get a match in the 4th case is that your search specs are not strict enough, you are searching for "30", which is in the 4th example :
    "ATALA Trans 0700000224499 5111892056353077"
    therefore, it does match, if you have stricter specifications, please submit a sample workbook so that we can see examples of matches an non-matches
    Last edited by dredwolf; 01-10-2014 at 12:03 AM.
    A picture may be worth a thousand words, BUT, a sample Workbook is worth a thousand screenshots!
    -Add a File - click advanced (next to quick post), scroll to manage attachments, click, select add files, click select files, select file, click upload, when file shows up at bottom left, click done (bottom right), click submit
    -To mark thread Solved- go top of thread,click Thread Tools,click Mark as Solved
    If you received helpful response, please remember to hit the * of that post

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: Formula(s) to extract string that matches certain criteria from a text

    If text starts from A1 put this in B1 and drag down.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-09-2014
    Location
    Jakarta
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Formula(s) to extract string that matches certain criteria from a text

    Thanks guys for your replies so far!
    I have no idea why I couldn't think of LEN() myself
    I attached an example workbook with the original solution, a modified one including the LEN() (#2) and the solution by kvsrinivasamurthy which however does not seem to work.
    #2 seems to provide reliable results so far, I'll do a little more testing but it looks promising!

    TextExtractExampleWB1.xlsx

  6. #6
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Formula(s) to extract string that matches certain criteria from a text

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    how bout MID instead of left/right/Trim combo

    have 2 If loops
    one to see if any "30" exist
    second to check the length criteria
    Last edited by humdingaling; 01-12-2014 at 08:12 PM.

  7. #7
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Formula(s) to extract string that matches certain criteria from a text

    =IFERROR(MID(A2,SEARCH("30???????",A2),9)+0,"not found") seems to work
    but on second look if there is another 30 in the cell all these formulas fail
    eg
    TRSF I-BAK CR 1912/FTSZE/W3251 302000.00 Mka Poluia Sode : 307827482
    Last edited by martindwilson; 01-12-2014 at 08:43 PM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  8. #8
    Registered User
    Join Date
    01-09-2014
    Location
    Jakarta
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Formula(s) to extract string that matches certain criteria from a text

    Thanks again for the further input!

    The MID() function seems indeed much easier, simple but very helpful!

    And Martin, thank you very much for the formula! I was trying to make it work with wildcards but somehow didn't manage to...
    But the problem you mentioned was exactly the one I just encountered with a different formula.
    Any further advice how to solve this problem?

  9. #9
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Formula(s) to extract string that matches certain criteria from a text

    This solution works for cases including examples like this:

    1) TRSF I-BAK CR 1912/FTSZE/W3251 301234.56 Mka Poluia Sode : 307827482
    2) TRSF I-BAK CR 1912/FTSZE/W3251 301234567 Mka Poluia Sode : 307827482
    In case 2, there are 2 matchs, take the last.

    Please Login or Register  to view this content.
    Quang PT

  10. #10
    Registered User
    Join Date
    01-09-2014
    Location
    Jakarta
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Formula(s) to extract string that matches certain criteria from a text

    Quote Originally Posted by bebo021999 View Post
    This solution works for cases including examples like this:

    1) TRSF I-BAK CR 1912/FTSZE/W3251 301234.56 Mka Poluia Sode : 307827482
    2) TRSF I-BAK CR 1912/FTSZE/W3251 301234567 Mka Poluia Sode : 307827482
    In case 2, there are 2 matchs, take the last.

    Please Login or Register  to view this content.

    Thank you very much!
    Even though I cannot really say that I understand the formula it works in most of the cases
    Would you mind explaining the functionality of this formula?

    But for example in the following case:
    Please Login or Register  to view this content.
    The formula gives the this output: 302543552

    However, as I now realize, in basically all the cases there is either a space or a line break before or after the number I search for.
    Sorry that I didn't include that above.

    So is there a way to ensure that either there is a space/line break before or after the number?

    (The formula now also gives out results that do not start with "30" and thus are not the number I search for, however I think that is pretty easy to solve).

    Thanks again!

  11. #11
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Formula(s) to extract string that matches certain criteria from a text

    #307827439 has no space before!

  12. #12
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Formula(s) to extract string that matches certain criteria from a text

    Quote Originally Posted by Turtler View Post
    Would you mind explaining the functionality of this formula?
    =LOOKUP(2,1/(LEN(--MID(A2,ROW(INDIRECT("1:"&LEN(A2))),9))=9),--MID(A2,ROW(INDIRECT("1:"&LEN(A2))),9))
    Assuming

    A2= abc 301234567 def

    we need to get 9-digit starts with 30 out

    Follow these steps:

    1) Go through all the cases of Text in A2 with LEN 9

    =MID(A2,ROW(INDIRECT("1:"&LEN(A2))),9)

    in which ROW(INDIRECT("1:"&LEN(A2))) establishes array of {1,2,3,...,17}

    Highlight the MID then F9 we get:

    {"abc 30123";"bc 301234";"c 3012345";" 30123456";"301234567";"01234567 ";"1234567 d";"234567 de";"34567 def";"4567 def";"567 def";"67 def";"7 def";" def";"def";"ef";"f"}

    To filter number with: --MID(....)

    {#VALUE!;#VALUE!;#VALUE!;30123456;301234567;1234567;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!}

    Now we know the number we need is in the 5th position.

    2)To marks the number position with LEN 9 by LEN(MID(...)=9

    {#VALUE!;#VALUE!;#VALUE!;FALSE;TRUE;FALSE;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!;#VALUE!}

    to exclude FALSE (=0) value, then remains TRUE (=1) only:
    1/LEN(...) =

    3) Use LOOKUP to get value

    =LOOKUP(2,1/LEN(),--MID())

    Hope it is clear for you.

  13. #13
    Forum Expert dredwolf's Avatar
    Join Date
    10-27-2012
    Location
    Clearwater,Canada
    MS-Off Ver
    Excel 2007
    Posts
    2,649

    Re: Formula(s) to extract string that matches certain criteria from a text

    As I believe I mentioned before, the more actual rules for the definition of the search text, the more refined the the solution can be, but without some examples of what you want, we are still "guessing" at what you expect...
    Please upload a sample so that we can see what you are getting, and what you need !

  14. #14
    Registered User
    Join Date
    01-09-2014
    Location
    Jakarta
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Formula(s) to extract string that matches certain criteria from a text

    Hey all, thanks so much again!

    Martin you're right, there are cases where there is no space, sorry about my mistake.

    Bebo, thank you very much for the explanations! They helped a lot!

    Dredwolf, sorry if my remarks weren't really clear.
    I myself do not really know how the cases in fact look like since they come from 3rd parties, I only know that in some cases there will be this number starting with "30" in there and in some cases not and I'm trying to get this number for the cases were it is included and have a error message where it is not included.
    I also uploaded an example workbook in my post from January 10. But I have attached an updated version with more solutions and also the one of Bebo included here: TextExtractExampleWB4.xlsx

    So now that a space is not valid for identifying the start of the number, let's assume that there has to be a non-numerical value in front of a number starting with "30" followed by 7 numerical digits, followed by nothing or a non-numerical value.

    In know it's quite confusing and you've been a great help already but perhaps someone has an idea on how to further tackle this problem

  15. #15
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Formula(s) to extract string that matches certain criteria from a text

    Updated version:
    Please Login or Register  to view this content.
    may works?

  16. #16
    Registered User
    Join Date
    01-09-2014
    Location
    Jakarta
    MS-Off Ver
    Excel 2013
    Posts
    6

    Re: Formula(s) to extract string that matches certain criteria from a text

    Quote Originally Posted by bebo021999 View Post
    Updated version:
    Please Login or Register  to view this content.
    may works?
    Thanks again bebo!

    Unfortunately, for the last example, the formula still gives out an incorrect result, see Workbook here: Attachment 292798.
    I don't know if that is possible (or if you already incorporated that) but can you include the check I described in my previous post in the formula?
    non-numerical value in front of a number starting with "30" followed by 7 numerical digits, followed by nothing or a non-numerical value
    This is unfortunately beyond my capabilities so I hope one of you guys can help me out!
    Thanks again so much for your efforts!

  17. #17
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: Formula(s) to extract string that matches certain criteria from a text

    Pl see attached file.
    Attached Files Attached Files
    Last edited by kvsrinivasamurthy; 01-27-2014 at 06:36 AM.

+ 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] Extract text from a string of text (amend formula to include new criteria)
    By robertguy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-10-2013, 04:53 PM
  2. what formula to extract text from a long string?
    By SEMMatt in forum Excel General
    Replies: 5
    Last Post: 10-07-2012, 10:23 PM
  3. formula to extract a section of of a text string
    By dcgrove in forum Excel General
    Replies: 6
    Last Post: 07-02-2010, 11:28 AM
  4. Extract Math Formula from text string
    By jpao in forum Excel General
    Replies: 2
    Last Post: 07-01-2010, 03:42 AM
  5. [SOLVED] Formula to extract digits from a text string?
    By [email protected] in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-15-2006, 12:16 AM

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