+ Reply to Thread
Results 1 to 6 of 6

Extracting Numbers from text string

  1. #1
    Registered User
    Join Date
    05-28-2007
    Posts
    2

    Extracting Numbers from text string

    I would a formula that will extract a number from a text string. I have written one but if the name in the text includes a number I get a value error.

    Example:
    Basement Finishing (3630) - this is all in one cell and I want to pull out the number in the parenthesis.

    I have written a formula that does this but if, for example, the cell reads Basement 2 Finishing (3630) I get a value error.

    Here is the formula (it is an array)

    =1*MID(B12,MATCH(TRUE,ISNUMBER(1*MID(B12,ROW($1:$500),1)),0),COUNT(1*MID(B12,ROW($1:$500),1)))

    Can anyone help?

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    This formula will extract anything between an opening and closing parenthesis set. Perhaps it will work for you....

    =MID(B12,FIND("(",B12)+1,FIND(")",B12)-FIND("(",B12)-1)


    assumption, only 1 opening/closing parenthesis set exists in the cell.
    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
    Banned User!
    Join Date
    10-14-2006
    Posts
    1,211
    Quote Originally Posted by rlane23
    I would a formula that will extract a number from a text string. I have written one but if the name in the text includes a number I get a value error.

    Example:
    Basement Finishing (3630) - this is all in one cell and I want to pull out the number in the parenthesis.

    I have written a formula that does this but if, for example, the cell reads Basement 2 Finishing (3630) I get a value error.

    Here is the formula (it is an array)

    =1*MID(B12,MATCH(TRUE,ISNUMBER(1*MID(B12,ROW($1:$500),1)),0),COUNT(1*MID(B12,ROW($1:$500),1)))

    Can anyone help?
    Try this:

    =MID(SUBSTITUTE(B12,")",""),FIND("(",B12)+1,99)+0
    Last edited by Teethless mama; 05-28-2007 at 03:07 PM.

  4. #4
    Registered User
    Join Date
    05-28-2007
    Posts
    2
    Thank you.
    Both examples work to extract what's in the parenthesis but what if there is a name in parenthesis before the number and I only want the number extracted? Is there anything I can do to only extract the number?
    Example:
    Duke And King ( Burger King- Bloomington ) (4228)

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Perhaps a User Defined Function (UDF). Insert the code below into a general module on the relevant workbook.

    Please Login or Register  to view this content.
    You can then invoke the function like

    =myfunc(A3)

    This worked for the 3 examples you have given.

    HTH

    rylo

  6. #6
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    If there's only one occurrence of a number in the string (ie the one you want to extract) then the following is another formula option:

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

    Hope this helps!

    Richard


    EDIT: Rylo, there are slighly easier ways of writing the pattern if using Regular Expressions, two of which are below:

    Please Login or Register  to view this content.
    GetNum1 simply extracts the first numerical string of digits it matches in the string. This would work in all the highlighted examples. GetNum2 specifically matches to a sequence of digits ending with a closing bracket ). There could still be times when either of these returned incorrect results but that depends on the structure of the string passed to the function and also exactly what the required return value would be in those cases).
    Last edited by Richard Schollar; 05-29-2007 at 05:19 AM.

+ 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