+ Reply to Thread
Results 1 to 9 of 9

Return the first number in a text string

  1. #1
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Return the first number in a text string

    Is there an easy method for returning the first number in a text string?

    If the string is --> it returns a Number.

    33A --> 33
    432ABC --> 432
    5Bob --> 5

    I thought there was an Excel function (might have been a VBA function) that would treat a text sting as a number and return the first number of the string. I can't seem to find it, hiding in my past memory.

    Thanks in advance. This might be very easy..
    Last edited by MarvinP; 04-23-2014 at 02:44 AM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  2. #2
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Return the first number in a text string

    Maybe this

    =REPLACE(A1,MATCH(1,(CODE(MID(A1,ROW($A$1:$A$255),1))<90)*(CODE(MID(A1,ROW($A$1:$A$255),1))>=65),0),255,"")+0

    A
    B
    1
    33A
    33
    2
    432ABC
    432
    3
    5Bob
    5


    Or this

    =MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),SUM(LEN(A1)-LEN(SUBSTITUTE(A1,{0,1,2,3,4,5,6,7,8,9},""))))+0
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  3. #3
    Forum Expert Ace_XL's Avatar
    Join Date
    06-04-2012
    Location
    UAE
    MS-Off Ver
    2016
    Posts
    6,074

    Re: Return the first number in a text string

    Try this array formula if there is only one number string

    =1*MID(A1,MATCH(TRUE,ISNUMBER(1*MID(A1,ROW($1:$255),1)),0),COUNT(1*MID(A1,ROW($1:$255),1)))

    See here
    http://office.microsoft.com/en-ca/ex...001154901.aspx

    or the age-old
    =LOOKUP(99^99,--("0"&MID(A1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0 123456789")),ROW($1:$10000))))
    Life's a spreadsheet, Excel!
    Say thanks, Click *

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Return the first number in a text string

    From
    http://www.excelforum.com/excel-form...ic-string.html

    =LOOKUP(99^99,--("0"&MID(J1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},J1&"0 123456789")),ROW($1:$10000))))

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Return the first number in a text string

    Here is my problem. Excel is so big and has so many functions, I can't retrieve what I thought was true or easier.

    After a lot of searching. I was remembering the VBA "Val()" function which takes the first part of a string, until it hits a letter, and returns the number equivalent.

    I thought, how simple this is and I should remember it for when I need it sometime. Find below the User Defined Function that does the work of those formulas above.

    Please Login or Register  to view this content.
    To use the above UDF you need to copy the above 3 line into a Module. Then
    Example: in Cell A1 you put "42 Is what I want!"
    In B1 you would use the UDF with the formula of "=Str2Num(A1)"

    I remember being intrigued on how easy the VBA Val() function could return the first number in a string while using normal Excel functions were much harder.

    I'd bet there is a Control+Shift+Enter function that would replace all letters with "" and give another easier answer without using VBA...???

  6. #6
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Return the first number in a text string

    Hi Marvin,

    Can you just clarify something?

    You say that you're looking for "the first number in a text string", yet your three examples all have the number at the very start of the string. Is it in fact just these string types you're interested in, or did you simply forget to give other examples where that is not the case?

    Cheers
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Return the first number in a text string

    Hi,

    Yes I meant first number before any letters. Like

    123abc456 --> 123

  8. #8
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Return the first number in a text string

    Ah, then in that case things are much simpler:

    =LOOKUP(1E+307,0+LEFT(A1,ROW($1:1000)))

    Edit: on second thoughts, this will fail for strings such as: 12-4, 1E3, etc.

    Regards
    Last edited by XOR LX; 04-22-2014 at 03:10 PM.

  9. #9
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Return the first number in a text string

    In that case:

    =LEFT(A1,MATCH(FALSE,INDEX(ISNUMBER(0+MID(A1,ROW($1:$1000),1)),,),0)-1)

    Regards

+ 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. Search for text in a string return value of the text string.
    By gallegosdavidm in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-21-2013, 10:55 AM
  2. [SOLVED] IF range of cells contains string of text, return contents of cell where string is found
    By nobodyukno in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-03-2013, 09:56 AM
  3. [SOLVED] Find partial text string within another text string return original text into cell.
    By mikey42979 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 06-17-2013, 02:58 PM
  4. [SOLVED] Return row number of text string
    By KimMorgan in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-04-2013, 07:17 PM
  5. Replies: 2
    Last Post: 02-26-2008, 03:22 PM

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