+ Reply to Thread
Results 1 to 18 of 18

Extracting a number out of a sentence

  1. #1
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Extracting a number out of a sentence

    Hi I am looking for an equation or macro that can extract numbers out of a sentence.

    Eg, 'There were 300 children in a school, and 10 classes of children' I need to extrcat the 300 and the 10 into 2 cells.

    There may be more than 2 numbers in longer sentences. Is there anyway to do this?

    Thanks in advance
    Last edited by Geomarsh; 08-12-2009 at 07:21 AM.

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Extracting a number out of a sentence

    If you download and install the free addin Morefunc.xll from here:

    http://download.cnet.com/Morefunc/30...-10423159.html


    then you can use this formula:

    =SUBSTITUTE(TRIM(REGEX.SUBSTITUTE(A1,"[^0-9]"," "))," ",",") copied down

    which extracts the numbers and separates them with a comma.

    or if you want them in separate cells, then use formula

    =TRIM(REGEX.SUBSTITUTE(A1,"[^0-9]"," ")) copied down

    and then select the column, go to Data|Text to Columns and select Delimited, click Next, select Space, Click Finish..
    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 martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting a number out of a sentence

    while were on reg ex why not
    =REGEX.MID($A1,"\b([0-9]|[1-9][0-9]|[1-9][0-9][0-9])\b",1,FALSE)
    which will extract any digit group fron 0-999 the numer in magenta is the instance so for the second change to 2 or use
    =REGEX.MID($A1,"\b([0-9]|[1-9][0-9]|[1-9][0-9][0-9])\b",COLUMNS($A$1:A1),FALSE) which can be dragged across and down
    "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

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

    Re: Extracting a number out of a sentence

    If your posted example is typical:
    Please Login or Register  to view this content.
    This formula pulls the first number:
    Please Login or Register  to view this content.

    and this one pulls the second:

    Please Login or Register  to view this content.
    Using the example...
    B1 returns: 300
    C1 returns: 10

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

  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Extracting a number out of a sentence

    Quote Originally Posted by Ron Coderre View Post
    If your posted example is typical:
    Please Login or Register  to view this content.
    This formula pulls the first number:
    Please Login or Register  to view this content.

    and this one pulls the second:

    Please Login or Register  to view this content.
    Using the example...
    B1 returns: 300
    C1 returns: 10

    Is that something you can work with
    I figured, based on this comment:

    There may be more than 2 numbers in longer sentences
    that it would be easier to do as I prescribed.... instead of having a big formula going across the sheet to cover all possible combinations...

  6. #6
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Extracting a number out of a sentence

    OK will have a go thanks!

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

    Re: Extracting a number out of a sentence

    Quote Originally Posted by NBVC View Post
    I figured, based on this comment:
    that it would be easier to do as I prescribed.... instead of having a big formula going across the sheet to cover all possible combinations...
    Good point. I agree.
    If only "regular expressions" were intuitive...instead of looking like compu-gibberish

  8. #8
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Extracting a number out of a sentence

    Quote Originally Posted by Ron Coderre View Post
    Good point. I agree.
    If only "regular expressions" were intuitive...instead of looking like compu-gibberish
    Amen to that... that is why I limit my regex suggestions... I have to figure it all out first...which takes a lot of time/practice.. for sure

  9. #9
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Extracting a number out of a sentence

    Thanks very much guys, Unfortunately couldn't download the things you said NBVC, as on work computer and it won't let me. The Lookup is working quite well though, I'm just slightly adapting (well, trying my best) for when there is more than 2 values.

    I'm not very good with computers, Im more maths-y minded!

  10. #10
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Extracting a number out of a sentence

    Well to get the 3rd, using Ron's Method... you would need to nest the Substitute() function:

    Please Login or Register  to view this content.
    and the 4th, yet another nested Substitute...

    Please Login or Register  to view this content.
    Hopefully, you can see the pattern.....

    ... but you will quickly get an big ugly formula, and unless you are in 2007, you will only have a limited number of nestings you can do...

    ..or.. maybe Ron can write you a UDF perhaps including the Regex functionality.

  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: Extracting a number out of a sentence

    If only "regular expressions" were intuitive...instead of looking like compu-gibberish
    look fine to me lol
    i just use a site that has them already done then modify as needed!

  12. #12
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Extracting a number out of a sentence

    Quote Originally Posted by martindwilson View Post
    look fine to me lol
    i just use a site that has them already done then modify as needed!
    care to share?

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

    Re: Extracting a number out of a sentence


  14. #14
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Extracting a number out of a sentence

    Thanks Martin.

  15. #15
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Extracting a number out of a sentence

    Thanks very much guys - really useful!!

  16. #16
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Extracting a number out of a sentence

    Sorry - Read post below - forgot to attach!
    Last edited by Geomarsh; 08-12-2009 at 07:24 AM. Reason: forgot to attach example

  17. #17
    Registered User
    Join Date
    07-17-2009
    Location
    Bristol England
    MS-Off Ver
    Excel 2007
    Posts
    46

    Re: Extracting a number out of a sentence

    Hi, I'm struggling with the equations now - think I'm just rubbish. But I have attached an example, but some of the numbers arn't coming out, and some are being completely missed!

    Any ideas?
    Attached Files Attached Files
    Last edited by Geomarsh; 08-12-2009 at 07:36 AM.

  18. #18
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Extracting a number out of a sentence

    This is happening because when a returned value ends with a 0 after the decimal point, it cuts that 0 off and when column B or C substitute that number, in the original text, then all the digits before the last 0 are substituted only, leaving a 0 behind in the original text and so the formula thinks that lone 0 is a number and extracts it...

    To resolve that, convert the number to replace to a text string forcing 2 decimal places.. so that the full number gets subbed including any possible ending 0... harder to explain than to see...

    anyways, replace formula in B1 with:

    Please Login or Register  to view this content.
    and formula in C1 with:

    Please Login or Register  to view this content.
    and copy them down.

    Note the red parts are the "fixed" parts...

    Also note, that you have many of these, the process may be slow...

    one way to reduce the slowness is to change the 1000 in the formulas to a number that represents the longest a string in column A may be... for example 200 characters?.. I used 255 in the attached sheet and it got all the numbers required faster...
    Attached Files Attached Files

+ 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