Nov
22

How to extract number from string in Excel

The tutorial shows how to extract number from various text strings in Excel by using formulas and the Extract tool.

When it comes to extracting part of a text string of a given length, Excel provides three Substring functions (Left, Right and Mid) to quickly handle the task. When it comes to extracting a number from an alphanumeric string, Microsoft Excel provides… nothing.

To get a number from a string in Excel, it takes a little ingenuity, a bit of patience, and a bunch of different functions nested into each other. Or, you can run the Extract tool and have the job done with a mouse click. Below you will find full details on both methods.

How to extract number from the end of text string

When you have a column of alphanumeric strings where number always comes after text, you can use one of the following formulas to get it.

Important note! In these formulas, the extraction is performed with the RIGHT function, which belongs to the category of Text functions. The output of this function is always text. In our case, the result will be a numeric substring, which in terms of Excel is also text, not number. On taking a close look at the returned values, you may notice a few visual indicators of text entries such as the default left alignment in cells, leading zeros, inability to use the extracted values in calculations, etc.

Formula 1. Get number from the right of a string

To extract number from a 'text-number' string, the first thing you need to know is where to start the extraction. So, let's determine the position of the first digit with this generic formula:

MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},cell&"0123456789"))

We will dwell on the formula's logic a bit later. For now, simply replace cell with a reference to the cell containing the original string (A2 in our case), and enter the formula in any empty cell in the same row, say in B2:

=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A2&"0123456789"))

Although the formula contains an array constant, it's a regular formula completed in the usual way by pressing the Enter key.

Once the position of the first digit is determined, you use the RIGHT function to extract the number. To find out how many characters should be extracted, you subtract the position of the first digit from the total length of the string, and add one to the result because the first digit is also to be included:

=RIGHT(B2, LEN(A2)-B2+1)

Where A2 is the original string and B2 is the position of the first digit.

The following screenshot shows the results:
Formula to extract number from the right of a string

To eliminate the helper column containing the position of the first digit, you can embed the MIN formula directly in the RIGHT function like this:

=RIGHT(A2,LEN(A2)-MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A2&"0123456789"))+1)

How the position of the 1st digit is calculated

We supply the array constant {0,1,2,3,4,5,6,7,8,9} in the find_text argument of the SEARCH function, so that the formula searches for each element of the array within the original string and returns their positions. Because the array constant contains 10 digits, the resulting array also contains 10 elements.

The MIN function processes the resultant array and returns the smallest value, which corresponds to the position in of the first digit in the original string.

Additionally, we use a special construction (A2&"0123456789") to concatenate every possible number with the original string. In situations when a certain number in the array constant is not found within the source string, this construction acts like IFERROR forcing the formula to return a "fake" position equal to the string length +1 or more chars.  As the result, if the original string does not contain any number, like in row 7 in the screenshot above, the RIGHT formula returns an empty string.

To make things easier to understand, let's see how the formula works out for a specific cell, say A2. That cell contains the text string "ECDAA-05", for which the SEARCH function returns the following array {7,10,11,12,13,8,15,16,17,18}. Here's how:

  • 0 is the 1st element of the array constant and the 7th character in the original string, therefore the first item of the resulting array is "7".
  • 5 is the 6th element of the array constant and the 8th character in the original string, so the sixth item of the resulting array is "8".
  •  No other item of the array constant is found in cell A2, and therefore the other 8 items of the resulting array represent the positions of corresponding digits in the concatenated string (ECDAA-050123456789).

Getting the position of the 1st digit in a string

Since 7 is the smallest value in the resulting array, the MIN function returns it, so we get the position of the first digit (0) in original string.

Formula 2. Pull number from the right of a string

Another way to extract number from the end of a string is by using this generic formula:

RIGHT(cell,SUM(LEN(cell) - LEN(SUBSTITUTE(cell, {"0","1","2","3","4","5","6","7","8","9"},""))))

With the original text string in A2, you enter the below formula in B2 or any other empty cell in the same row, and then copy it down the column:

=RIGHT(A2,SUM(LEN(A2) - LEN(SUBSTITUTE(A2, {"0","1","2","3","4","5","6","7","8","9"},""))))

Another formula to get number from the end of a string

How this formula works

In essence, the formula searches for all numbers from 0 to 9 within the source string, counts the found numbers, and returns that many characters from the end of the string.

And here is the detailed formula break down:

  • First, you use the LEN and SUBSTITUTE functions to find out how many times a given number occurs in the string. For this, you replace the number with an empty string (""), and then subtract the length of the string without that number from the total length of the original string. This operation is performed on each number in the array constant.
  • Next, the SUM function adds up all occurrences of all digits in the string.
  • Finally, the digit count goes to the num_chars argument of the RIGHT function instructing it to return that many characters from the right side of the string.

How to extract number from the beginning of text string

If you are working with strings where text appears after number, the formula for extracting number would be similar to the one discussed above, with the difference that you use the LEFT function to pull the characters from the left side of the string:

LEFT(cell,SUM(LEN(cell)-LEN(SUBSTITUTE(cell,{"0","1","2","3","4","5","6","7","8","9"},""))))

With the original string in A2, use the following formula to get number:

=LEFT(A2,SUM(LEN(A2)-LEN(SUBSTITUTE(A2,{"0","1","2","3","4","5","6","7","8","9"},""))))

Formula to extract number from the beginning of text string

Note. As is the case with the RIGHT function, the LEFT function returns a numeric substring, which is technically text, not number.

How to get number from any position in a string

If your task implies extracting number from anywhere in a string, you can make use of the following mind-boggling formula published on MrExcel forum:

=SUMPRODUCT(MID(0&A2, LARGE(INDEX(ISNUMBER(--MID(A2, ROW(INDIRECT("1:"&LEN(A2))), 1)) * ROW(INDIRECT("1:"&LEN(A2))), 0), ROW(INDIRECT("1:"&LEN(A2))))+1, 1) * 10^ROW(INDIRECT("1:"&LEN(A2)))/10)

Where A2 is the original text string.

Breaking down this formula would require a separate article, so you can simply copy it to your worksheet to make sure it really works :)
Formula to get number from any position in a string

Upon examining the results, however, you may notice one insignificant drawback - if the source string does not contain a number, the formula returns zero, as in row 6 in the screenshot above. To fix this, you can wrap the formula in the IF statement, the logical test of which checks if the source string contains any number. If it does, the formula extracts the number, otherwise returns an empty string:

=IF(SUM(LEN(A2)-LEN(SUBSTITUTE(A2, {"0","1","2","3","4","5","6","7","8","9"}, "")))>0, SUMPRODUCT(MID(0&A2, LARGE(INDEX(ISNUMBER(--MID(A2,ROW(INDIRECT("$1:$"&LEN(A2))),1))* ROW(INDIRECT("$1:$"&LEN(A2))),0), ROW(INDIRECT("$1:$"&LEN(A2))))+1,1) * 10^ROW(INDIRECT("$1:$"&LEN(A2)))/10),"")

As shown in the screenshot below, the improved formula works beautifully (kudos to Alex, our Excel guru, for this improvement):

An improved formula to extract number from anywhere in a string

Unlike in all previous examples, the result of this formula is number. To make sure of this, just notice the right-aligned values in column B and truncated leading zeros.

This is how you can extract number from string in Excel. To better understand and probably reverse-engineer the formulas, you are welcome to download our sample Excel Extract Number workbook.

Extract number from text string with Ultimate Suite

As you have just seen, there is no trivial Excel formula to pull number from a text string. If you have difficulties with understanding the formulas or tweaking them for your data sets, you may like this simple way to get number from string in Excel.

With our Ultimate Suite added to your Excel ribbon, this is how you can quickly retrieve number from any alphanumeric string:

  1. Go to the Ablebits Data tab > Text group, and click Extract:
    Extract tool for Excel
  2. Select all cells with the source strings.
  3. On the Extract tool's pane, select the Extract numbers radio button.
  4. Depending on whether you want the results to be formulas or values, select the Insert as formula box or leave it unselected (default).

    My advice is to select this box if you want the extracted numbers to update automatically as soon as any changes are made to the source strings. If you want the results to be independent on the original strings (e.g. in case you plan to remove the source data at a later point), then do not select this box.

  5. Click the Insert Results button. Done!

Extract numbers and insert the results as formulas or values.

Like in the previous example, the results of the extraction are numbers, meaning you are free to count, sum, average, or perform any other calculations with them.

In this example, we've chosen to insert the results as values, and the add-in did exactly what was asked for:
Numbers are extracted from strings as values.

If we selected the Insert as formula checkbox, you'd observe a formula in the formula bar. Curious to know which one? Just download Ultimate Suite's trial and see for yourself :)

If you'd like to have this as well as 60+ more useful tools in your Excel, be sure to grab the 15% off coupon code that we provide exclusively to our blog readers: AB14-BlogSpo.

I thank you for reading and hope to see you on our blog next week!

You may also be interested in:

6 Responses to "How to extract number from string in Excel"

  1. ramkey says:

    Input result
    1 (21) 500 555-0145----> 5005550117
    Write a formula to extract the numbers, eliminating all the spaces symbols state codes
    for eg eg: 1 (12) 500 555-0117 shall change to 5005550117

    thanks for advance
    regards
    RAMKEY

    my requiremnt is

  2. Hello, Ramkey,

    The formula below should help you:
    =SUMPRODUCT(MID(0&RIGHT(A1,LEN(A1)-FIND(")",A1)), LARGE(INDEX(ISNUMBER(--MID(RIGHT(A1,LEN(A1)-FIND(")",A1)), ROW(INDIRECT("1:"&LEN(RIGHT(A1,LEN(A1)-FIND(")",A1))))), 1))*ROW(INDIRECT("1:"&LEN(RIGHT(A1,LEN(A1)-FIND(")",A1))))), 0), ROW(INDIRECT("1:"&LEN(RIGHT(A1,LEN(A1)-FIND(")",A1))))))+1, 1) * 10^ROW(INDIRECT("1:"&LEN(RIGHT(A1,LEN(A1)-FIND(")",A1)))))/10)

    If you need a quicker solution, then you can use our add-in. The last point of the article above explains how it works.

    Hope this helps!

  3. Vitaly says:

    Dear Ladies,

    Could you please help with the formulas?
    The input is LME_04329_100_VSH_04122017_SHUBIN_AMEND
    I need to extract the date and surname in separate cells.
    04122017 and SHUBIN

    Thank you in advance!

    Vitaly

    • Hi Vitaly,

      With input in A2, the formulas go as follows:

      Extract the date:

      =MID(A2, FIND(CHAR(1),SUBSTITUTE(A2,"_",CHAR(1),4))+1, FIND(CHAR(1),SUBSTITUTE(A2,"_",CHAR(1),5)) - FIND(CHAR(1),SUBSTITUTE(A2,"_",CHAR(1),4))-1)

      Extract the surname:

      =MID(A2, FIND(CHAR(1),SUBSTITUTE(A2,"_",CHAR(1),5))+1, FIND(CHAR(1),SUBSTITUTE(A2,"_",CHAR(1),6)) - FIND(CHAR(1),SUBSTITUTE(A2,"_",CHAR(1),5))-1)

      Please note, the formulas work only for strings of the same pattern, i.e. a date is always between the 4th and 5th underscores and a surname is between the 5th and 6th underscores.

  4. Vitaly says:

    Dear Svetlana!
    Thank you so much!!!

    Best regards,
    Vitaly

  5. Forse says:

    Dears ,

    Could you please help with the formulas?
    in cell A1 i input Forse41.24 and in cell B1 i input Forse 41.24
    I need to extract the number and text in separate cells.
    41.24 and Forse

    Thank you in advance!

    Forse

Post a comment



Unfortunately, due to the volume of comments received we cannot guarantee that we will be able to give you a timely response. When posting a question, please be very clear and concise. We thank you for understanding!
Excel add-ins and Outlook tools - Ablebits.com
Ultimate Suite for Excel Professionals
 
 
60+ professional tools for Excel 2016-2007 to do your daily work
Incredible product, even better tech support…AbleBits totally delivers!
Deborah Bryant
Anyone who works with Excel is sure to find their work made easier
Jackie Lee
The best spent money on software I've ever spent!
Patrick Raugh
Ablebits is a fantastic product - easy to use and so efficient.
Debra Celmer
Excel is at its best now
Annie C.
I don't know how to thank you enough for your Excel add-ins
Jennifer Morningstar
Anybody who experiences it, is bound to love it!
Kumar Nepa
AbleBits suite has really helped me when I was in a crunch!
Nelda Fink
I have enjoyed every bit of it and time am using it
Christian Onyekachi Nwosu
It's the best $100 we've ever spent!
Mike Cavanagh
I love the program, and I can't imagine using Excel without it!
Robert Madsen
One word… WOW!
Dave Brown
Love the products!
David Johnston
It is like having an expert at my shoulder helping me…
Linda Shakespeare
Your software really helps make my job easier
Jeannie C.
Thanks for a terrific product that is worth every single cent!
Dianne Young
I love your product
Brad Gibson
Awesome!!!
Sheila Blanchard