+ Reply to Thread
Results 1 to 28 of 28

Extract fixed number from variable text strings

  1. #1
    Registered User
    Join Date
    04-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    16

    Extract fixed number from variable text strings

    Hi,

    Does anybody know if there's a way to extract a number from a text string containing it, for example:

    randomtextstringhere12345678anotherrandomtextstringhere
    randomtextstringhereagain24682468anotherrandomtextstring
    randomtext13579135anotherrandomtextstringhere

    So the number is a fixed length but can appear anywhere in the text and the text is variable in length.

  2. #2
    Registered User
    Join Date
    04-01-2008
    Location
    Houston
    MS-Off Ver
    Office 365
    Posts
    85

    Re: Extract fixed number from variable text strings

    There are many ways to do this, here's one. where A1 is your data..

    =LOOKUP(9.99E+307,--MID(A1,MIN(FIND({1,2,3,4,5,6,7,8,9,0},A1&1234567890)),ROW(INDIRECT("1:"&LEN(A1)))))

    Hope this helps,
    Cullen

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extract fixed number from variable text strings

    Or without using INDIRECT()
    Please Login or Register  to view this content.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  4. #4
    Forum Expert icestationzbra's Avatar
    Join Date
    01-07-2004
    MS-Off Ver
    2007, 2010
    Posts
    1,421

    Re: Extract fixed number from variable text strings

    will you ever have a situation where you have string like this:

    randomtextstringhere12345678anotherrandomtextstringhere000999

    i.e., numbers appearing at various places in the string?
    - i.s.z -
    CSE, aka Array aka { }, formulae are confirmed with CONTROL+SHIFT+ENTER.
    Replace commas ( , ) with semicolons ( ; ) in formulae, if your locale setting demands.
    All good ideas are courtesy resources from this forum as well as others around the web.
    - e.o.m -

  5. #5
    Registered User
    Join Date
    04-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Extract fixed number from variable text strings

    Hi Cullen,

    That is great, thanks for your help. The only slight problem with it, that affects some of the data, is that sometimes there are other numbers in the text string before the number I'm looking for. Is there any way that I can target only say a 12 digit number in the text?

    Sorry about not putting this in the original question but I didn't realise it would be a problem until I ran your solution.

    Thanks,
    Ian

  6. #6
    Registered User
    Join Date
    04-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by icestationzbra View Post
    will you ever have a situation where you have string like this:

    randomtextstringhere12345678anotherrandomtextstringhere000999

    i.e., numbers appearing at various places in the string?


    Hi, no probably not but I will have occasions where there are numbers before the one I'm trying to extract.

  7. #7
    Registered User
    Join Date
    04-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by Marcol View Post
    Or without using INDIRECT()
    Please Login or Register  to view this content.
    Hi, thanks that works too.

  8. #8
    Forum Expert icestationzbra's Avatar
    Join Date
    01-07-2004
    MS-Off Ver
    2007, 2010
    Posts
    1,421

    Re: Extract fixed number from variable text strings

    you mean something like:

    000999randomtextstringhere12345678anotherrandomtextstringhere

    my point is to check whether you will ever have situation where the numbers will be separated by alphabets.
    Last edited by icestationzbra; 05-13-2012 at 09:51 PM.

  9. #9
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Extract fixed number from variable text strings

    With
    A1 containing your text string with embedded numbers.....like: 1234randomtext135791351234anotherrandomtextstringhere
    and
    D1 containing the number length you want to find: 12

    This regular formula returns the first number of that length found in A1
    Please Login or Register  to view this content.
    )

    In the above example, that formula returns: 135791351234

    is that something you can work with?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  10. #10
    Forum Expert
    Join Date
    12-03-2009
    Location
    Florence, Italy
    MS-Off Ver
    Excel 2019
    Posts
    1,796

    Re: Extract fixed number from variable text strings

    Hi,

    another option to identify first 12 digits number in a string:

    Please Login or Register  to view this content.
    to be confirmed with control+shift+enter

    Hope it helps

    Regards
    Attached Files Attached Files
    Last edited by canapone; 05-14-2012 at 04:33 AM. Reason: Small attachment
    -----------------------------------------------------

    At Excelforum, you can say "Thank you!" by clicking the star icon ("Add Reputation") below the post.

    Please, mark your thread [SOLVED] if you received your answer.

  11. #11
    Registered User
    Join Date
    04-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by Ron Coderre View Post
    With
    A1 containing your text string with embedded numbers.....like: 1234randomtext135791351234anotherrandomtextstringhere
    and
    D1 containing the number length you want to find: 12

    This regular formula returns the first number of that length found in A1
    Please Login or Register  to view this content.
    )

    In the above example, that formula returns: 135791351234

    is that something you can work with?
    Hi Ron,

    Thanks for that but I got an error when I tried it. I simplified it so that I only had one column, A, with the data in it then I copied your formula directly into B1 and put 12 in D1. The error highlighted LEN in your formula.

  12. #12
    Registered User
    Join Date
    04-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by CANAPONE View Post
    Hi,

    another option to identify first 12 digits number in a string:

    Please Login or Register  to view this content.
    to be confirmed with control+shift+enter

    Hope it helps

    Regards
    Hi Canapone,

    Thanks for your help, when I tried this I got a 0 in each result.

  13. #13
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Extract fixed number from variable text strings

    Did you include a closing bracket on Ron's suggestion? It isn't showing in your quote in post #11 and in Ron's post (#9) it shows below the formula. It somehow escaped the code tags!

  14. #14
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by IM22 View Post
    Hi Ron,

    Thanks for that but I got an error when I tried it. I simplified it so that I only had one column, A, with the data in it then I copied your formula directly into B1 and put 12 in D1. The error highlighted LEN in your formula.
    Hmmm...the only thing I see missing is the ending parenthesis after the final $D$1:
    Please Login or Register  to view this content.
    Did you try copying the formula from this web page?

  15. #15
    Registered User
    Join Date
    04-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by Cutter View Post
    Did you include a closing bracket on Ron's suggestion? It isn't showing in your quote in post #11 and in Ron's post (#9) it shows below the formula. It somehow escaped the code tags!
    I didn't notice that, but when I tried it with the closing bracket I still get the same error.

  16. #16
    Registered User
    Join Date
    04-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Extract fixed number from variable text strings

    Here's a screenshot of what I get:

    Excel formula 12 digit number extract.png

  17. #17
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by IM22 View Post
    I didn't notice that, but when I tried it with the closing bracket I still get the same error.
    Here you go...Excel 2000/2003 sometime has an issue when there are multiple instances of INDEX in a formula.
    Try this ARRAY FORMULA...committed with CTRL+SHIFT+ENTER (instead of just ENTER)
    Please Login or Register  to view this content.
    Does that work?

  18. #18
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by IM22 View Post
    I didn't notice that, but when I tried it with the closing bracket I still get the same error.
    And...Here's a regular formula that works:
    Please Login or Register  to view this content.
    Those INDEX functions can be really helpful to avoid C+S+E...But sometimes there's an art to placing them.

  19. #19
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extract fixed number from variable text strings

    ...But sometimes there's an art to placing them.
    Interesting Ron, always wondered about that feature in 2003. I thought it was just me being stupid!

    A couple of notes on your last formula.
    It returns a string not a number, this is easily solved by adding *1, however ...
    1/. If the string has a string of digits more than the requested length then a truncated result is returned.
    e.g.
    Request 8 digits from "randomtextstringhereagain246824689anotherrandomtextstring", "24682468" will be returned.

    2/. Because you are returning a string spaces seem to be treated as zero, so digits preceded with a space throw anomalies.
    e.g.
    Request 8 digits from "randomtextstringhereagain 246824689anotherrandomtextstring", " 2468246" will be returned.
    If there are extra spaces the result changes
    Request 8 digits from "randomtextstringhereagain(2 spaces)246824689anotherrandomtextstring", "(2 spaces)246824" will be returned.

    This is not a criticism, just an observation, it might be irrelevant to the OP.
    I'm looking for a more robust solution but am a bit pressed for time at the moment.

    There are also anomalies with CANAPONEs' solution when the string of digits exceeds the requested length
    e.g.
    Request 8 digits from "randomtextstringhereagain246824689anotherrandomtextstring", 46824689 will be returned which is larger than 24682468.

    If it's possible to have a decimal number in the string, well that's another kettle of fish!

    See the attached
    Attached Files Attached Files

  20. #20
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Extract fixed number from variable text strings

    can you use a udf with RegExp?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  21. #21
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extract fixed number from variable text strings

    This UDF will return the first group with the exact number of digits called.
    Option Explicit

    Please Login or Register  to view this content.
    Enter as e.g.
    Please Login or Register  to view this content.


    See this thread this and for other possible solutions by DonkeyOte
    Extract 9 digit number from string

  22. #22
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Extract fixed number from variable text strings

    "It returns a string not a number, this is easily solved by adding *1, however ..."
    >>I'm assuming we're looking for some kind of reference "number" that may have preceeding zeros.

    "1/. If the string has a string of digits more than the requested length then a truncated result is returned.
    e.g.
    Request 8 digits from "randomtextstringhereagain246824689anotherrandomtextstring", "24682468" will be returned."
    >> I don't think we've been given the complete set of requirements, yet and we don't know the ultimate purpose of the formula.


    "2/. Because you are returning a string spaces seem to be treated as zero, so digits preceded with a space throw anomalies.
    e.g."
    >> So far...no indication that spaces will be included. However, sometimes all we can do is toss out suggestions in hopes that one will be appropriate.

  23. #23
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Extract fixed number from variable text strings

    My preference (I like to keep things simple and complex formulas hurt my head )

    Please Login or Register  to view this content.

  24. #24
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extract fixed number from variable text strings

    @ Kyle123
    Again we don't know, but if there is a string of digits longer than the requested length, your UDF will return a truncated result.
    e.g.
    Request 8 digits from "randomtextstringhereagain246824689anotherrandomtextstring" ...
    Please Login or Register  to view this content.
    24682468 will be returned.
    Post #21 will reject this possibility.

    We need some feedback from the OP

  25. #25
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Extract fixed number from variable text strings

    @Marcol, good catch , hows this?
    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    04-07-2012
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    16

    Re: Extract fixed number from variable text strings

    Thanks a lot to everyone who has offered advice.

    Ron, your solutions both worked perfectly for my purposes, which as some were asking is to extract a 12 digit serial number from anywhere in a web URL. There would therefore be no spaces so that question does not matter to me although I can see why it would matter in other situations.

    As a matter of interest (or not!), Ron's solution does not work if the text ends with the 12 digit number, it needs for there to be text following it but again that doesn't matter to me.

    Thanks again to all, your help is much appreciated.

    Ian

  27. #27
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by IM22 View Post
    As a matter of interest (or not!), Ron's solution does not work if the text ends with the 12 digit number, it needs for there to be text following it but again that doesn't matter to me.

    Ian
    1) You're very welcome.
    2) Thanks for spotting a flaw in the formula I posted.
    This tweaked version resolves that issue:
    B1: =MID(A1,MATCH(1,INDEX(--ISNUMBER(--MID(A1,ROW(INDIRECT("1:"&LEN(A1)-$D$1+1)),$D$1)),0),0),$D$1)

  28. #28
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Extract fixed number from variable text strings

    Quote Originally Posted by IM22 View Post
    Is there any way that I can target only say a 12 digit number in the text?
    =GetNumber(A1,12)

    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)

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