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 numbers 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 comes after text, you can use the following formula to get it.

RIGHT(cell, LEN(cell) - MAX(IF(ISNUMBER(MID(cell, ROW(INDIRECT("1:"&LEN(cell))), 1) *1)=FALSE, ROW(INDIRECT("1:"&LEN(cell))), 0)))

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:

=RIGHT(A2, LEN(A2) - MAX(IF(ISNUMBER(MID(A2, ROW(INDIRECT("1:"&LEN(A2))), 1) *1)=FALSE, ROW(INDIRECT("1:"&LEN(A2))), 0)))

This formula gets number only from the end. If a string also has numbers in the beginning or middle, they are ignored:
alt=

The extraction is performed with the RIGHT function that belongs to the category of Text functions. The output of this function is always text. In our case, the result is a numeric substring, which in terms of Excel is also text, not number.

If you need the result to be a number (that you can use in further calculations), then wrap the formula into the VALUE function or perform an arithmetic operation that does not change the result, say, multiply by 1 or add 0. To catch errors in the strings that do not contain a single number, use the IFERROR function. For example:

=IFERROR(VALUE(RIGHT(A2, LEN(A2) - MAX(IF(ISNUMBER(MID(A2, ROW(INDIRECT("1:"&LEN(A2))), 1)*1)=FALSE, ROW(INDIRECT("1:"&LEN(A2))), 0)))), "")

or

=IFERROR(RIGHT(A2, LEN(A2) - MAX(IF(ISNUMBER(MID(A2, ROW(INDIRECT("1:"&LEN(A2))), 1) *1)=FALSE, ROW(INDIRECT("1:"&LEN(A2))), 0))) +0, "")
An improved formula to extract number from the end of a string

Note. In Dynamic Array Excel (Office 365 and 2021), you enter the formula in the usual way with the Enter key. In Excel 2019 and earlier, it only works as an array formula, so remember to press Ctrl + Shift + Enter to complete it.

How this formula works:

To extract number from an alphanumeric string, the first thing you need to know is where to start the extraction. The position of the last non-numeric character in a string is determined with the help of this tricky formula:

MAX(IF(ISNUMBER(MID(A2, ROW(INDIRECT("1:"&LEN(A2))), 1)*1)=FALSE, ROW(INDIRECT("1:"&LEN(A2))), 0))

To understand the logic, let's investigate it from the inside:

The ROW(INDIRECT("1:"&LEN(A2))) combination creates a sequence of numbers corresponding to the total of characters in the source string (A2), and we serve these sequential numbers to MID as the starting numbers:

MID(A2, {1;2;3;4;5;6;7;8}, 1)

The MID function pulls each individual character from A2 and returns them as an array:

{"0";"5";"-";"E";"C";"-";"0";"1"}

As MID is a text function, its output is always text (as you can notice, all the characters are enclosed in quotation marks). To turn numeric ones into numbers, we multiply the array by 1 (double negation --MID() will have the same effect). The result of this operation is an array of numbers and #VALUE! errors representing non-numeric characters:

ISNUMBER({0;5;#VALUE!;#VALUE!;#VALUE!;#VALUE!;0;1})

The ISNUMBER function evaluates each element of the array and gives its verdict in the form of Boolean values - TRUE for numbers, FALSE for anything else:

{TRUE;TRUE;FALSE;FALSE;FALSE;FALSE;TRUE;TRUE}

This array goes to the logical test of the IF function, where each element of the array is compared against FALSE:

IF({TRUE;TRUE;FALSE;FALSE;FALSE;FALSE;TRUE;TRUE}=FALSE, ROW(INDIRECT("1:"&LEN(A2))), 0)

For each FALSE (non-numeric value), another ROW(INDIRECT()) function returns its relative position in the string. For each TRUE (numeric value), a zero is returned. The resulting array looks as follows:

{0;0;3;4;5;6;0;0}

The rest is easy. The MAX function finds the highest number in the above array, which is the position of the last non-numeric value in the string (6 in our case). Simply, subtract that position from the total length of the string returned by LEN, and pass the result to RIGHT to let it know how many characters to extract from the right side of the string:

RIGHT(A2, LEN(A2) - 6)

Done!

How to extract number from the beginning of text string

If you are working with records where text appears after number, you can extract number from the start of a string by using this generic formula:

LEFT(cell, MATCH(FALSE, ISNUMBER(MID(cell, ROW(INDIRECT("1:"&LEN(cell)+1)), 1) *1), 0) -1)

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

=LEFT(A2, MATCH(FALSE, ISNUMBER(MID(A2, ROW(INDIRECT("1:"&LEN(A2)+1)), 1) *1), 0) -1)

No matter how many digits are in the middle or end, only the starting number is extracted:
Formula to extract number from the beginning of text string

Note. In Excel 365 and Excel 2021, due to support for dynamic arrays, a regular formula works fine. In Excel 2019 and earlier, you should press Ctrl + Shift + Enter to explicitly make it an array formula.

How this formula works:

Here, we again use the combination of ROW, INDIRECT and LEN functions to create a sequence of numbers equal to the total of characters in the source string plus 1 (the role of that additional character will become clear a bit later).

ROW(INDIRECT("1:"&LEN(A2)+1))

MID and ISNUMBER do the same job as in the previous example - MID pulls individual characters and ISNUMBER converts them to the logical values. The resulting array of TRUE's and FALSE's goes to the MATCH function as a lookup array:

MATCH(FALSE, {TRUE;TRUE;FALSE;FALSE;FALSE;FALSE;TRUE;TRUE;FALSE}, 0)

MATCH computes a relative position of the first FALSE, giving us the position of the first non-numeric character in the string (3 in A2). To extract the preceding numbers, we subtract 1 from position the first text character and serve the difference to the num_chars argument of the LEFT function:

LEFT(A2, 3-1)

Now, back to an "extra" character in the sequence generated by ROW(INDIRECT()+1)). As you already know, this sequence provides the starting points for the MID function. Without +1, MID would extract exactly as many characters as there are in the original string. If the string contains only numbers, ISNUMBER will return only TRUE's while MATCH needs at least one FALSE. To ensure that, we add one more character to the total length of the string, which the MID function would convert to an empty string. For example, in B7, MID returns this array:

{"1";"2";"3";"4";""}

Note. As is the case with the RIGHT function, LEFT also returns a numeric substring, which is technically text, not number. To get the result as a number rather than a numeric string, nest the formula in the VALUE function or multiply the result by 1 as shown in the first example.

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.

Tip. In Excel 365 - Excel 2019, there is a much simpler solution with the help of the TEXTJOIN function. Please see How to remove text and keep numbers.

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 the Insert as formula checkbox was selected, you'd observe a formula in the formula bar. Curious to know which one? Just download Ultimate Suite's trial and see for yourself :)

Available downloads

Excel Extract Number - sample workbook (.xlsx file)
Ultimate Suite - trial version (.exe file)

562 comments

  1. how to extract number from this string "1 PKt- Tomato 1057 - 20 K Seeds - 10850 Rs / on invoice 11000
    7 Pkt- Chilli Teja - 4 Seeds - 385 Rs / on invoice 400"

    result should be
    1 11000 10850 150 150
    7 400 385 15 105

  2. Hello, how can I pull this date from string 2/23/2078 ADM to 2/23/2078.

    PS this is bikaram shambat
    Waiting for response.

  3. Thanks it really worked.

  4. How would I extract only the largest number from a text string? Eg. if I had a string such as asd7adh8ash6 and I need just the 8 extracted.

    • Hello!
      Extract the numbers from the text as described in the article above and use the MAX function to choose the maximum.

      =MAX(IF(ISNUMBER(--MID(A1,ROW($1:$94),1)), --MID(A1,ROW($1:$94),1)," "))

      • This doesn’t seem to work as I keep getting a #VALUE! error.

        • I now have gotten it to work. However, I sometimes have double digit numbers that I need to extract. Ex adhh6asbf17asd5
          So, I need 17 rather than 7. Thanks for your help!

          • Hi!
            I'm sorry you're not asking the right question.
            To extract the maximum two-digit number from text, try the formula:

            =MAX(IF(ISNUMBER(--MID(A1,ROW($1:$100),2)),--MID(A1,ROW($1:$100),2),""))

  5. Hello,

    How can I get the string of numbers from this text to populate with the formula but also include the dash

    Ex. 51-1780 Expenses Parent : Travel Expense

    I used the following formula and it worked but I need the dash

    • Hello!
      To extract numbers from the text along with dash, try the formula

      =TEXTJOIN("",TRUE, IFERROR(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)*1, IF(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)="-","-","")))

      • Sorry replied to the wrong question

      • Thanks, Alexander,

        Excel shows me "You've entered too few arguments for this function"

          • Thanks for the swift answer, Alex,

            I found out the issue, it works now, but it takes all the numbers in a string and merges them into one. How can I take only the last 14 symbols (with dashes) from the right side of the cell?

            Thank you,
            Eldar

  6. Hi, the formula =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),"") does not work on numbers with decimals.

    • Hi!
      This formula cannot extract the decimal number because the decimal separator is not a digit.
      To extract a delimited number from text, try this formula:

      =TEXTJOIN("",TRUE, IFERROR(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)*1, IF(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)=".",".","")))

  7. Billed days vs Amount of a person I have put in a concatenated formula:
    {=CONCATENATE(COUNTIF(E4:AI4,8)+(COUNTIF(E4:AI4,4)/2),"/",(IFERROR(VLOOKUP(C4,'Master'!$C$4:$P$35,14,FALSE)*(COUNTIF(E4:AI4,8)+(COUNTIF(E4:AI4,4)/2)),"")))}

    Value comes to be 21/3525
    Using your formula {=VALUE(RIGHT(D4,LEN(D4)-3))} I get the value to be 3525 !!! PERFECT

    But if there is half day of resource the above value becomes 20.5/3125, using the same {=VALUE(RIGHT(D4,LEN(D4)-3))} formula, I get a number 9852358 or so... This disturbs the entire calculation for the month!

    What formula can I use to get the right cost if a person takes half day off (so 4 hours instead of 8) so the monthly cost comes right?

      • @Alexander Trifuntov - The solution worked !!!
        THANK YOU

  8. Hiya,

    Do you know how I can extract the first number in a string? Example below

    Cell value = dasd adas adsad 001234 dasd adas adsa 06253
    Expected result = 001234

    Thanks,

    Kim

  9. Hi, I used your formula for my data, and it works!
    But the problem is the number extracted is only the number, how if I need to extract number with point?

    For example:
    "afhb jafbjdk 2.60sad aksd" to be "2.60"

    Thank you

    • Hello!
      To extract all numbers and dots from text try this formula -

      =TEXTJOIN("",TRUE, IFERROR(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)*1, IF(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)=".", ".","")))

      I hope it’ll be helpful.

  10. i have lot of different word, like these 08048K0922, 45002CVC922, 9.5050K0722, 10.505CVC0722 i want to separate text specific value from that words like 08K,45CVC or 45CV, 9.5K, 10.5CV or CVC. how can i doing this?

  11. how to separate only numbers from mixed letters from a cell in excel.
    NEFT/TB/AXTB222710184240/AMARF

  12. This is sheet Winter ParkDownload cell B2

    Private Dbl Queen Room Deposit (Amount: 750.00 US
    Arrive on Saturday (Amount: 240.00 USD)
    Sunday Lift Ticket (Amount: 160.00 USD)
    Service Fee (Amount: 15.00 USD)
    Total: 1,165.00 USD

    Is there a formula to take the Total 1,16500 in cell B2 to another tab on the same sheet called WinterPark, Cell M8. I only want to have the Total $ amount to the cell not the text "total" Any help would be greatly appreciated. Thank you! Chuck

  13. HOW CAN WE EXRACT THE SPECFIC NUMERIC AND TEXT FROM THIS STRING "EXIT 2000x500 KB"
    OUTPUT SHOULD "2000x500".. PLEASE SOMEONE SHOULD EXPLAIN THIS QUERY.

  14. I need to extract a number from a string of text and the number is sometimes negative and sometimes positive. Can these formulas be modified to capture the negative sign with the number? Thank you! p.s. I love the ablebits Excel add-in!

    • Hello!
      To extract a negative number from text, try this formula:

      =--(IF(MID(A1;MIN(FIND({0\1\2\3\4\5\6\7\8\9};A1&"_0123456789"))-1;1)="-"; MID(A1;MIN(FIND({0\1\2\3\4\5\6\7\8\9};A1&"_0123456789"))-1;1);"") & SUMPRODUCT(MID(0&A1; LARGE(INDEX(ISNUMBER(--MID(A1;ROW(INDIRECT("1:"&LEN(A1)));1))*ROW(INDIRECT("1:"&LEN(A1)));0); ROW(INDIRECT("1:"&LEN(A1))))+1;1)*10^ROW(INDIRECT("1:"&LEN(A1)))/10))

      I hope it’ll be helpful.

  15. I have a cell valeu as 1 hr 30 min or 4 hours and other as 4 hrs 20 mins, I need to extracrt hr value in one coloumn and min vaue in other colomn, to calculate the total time elapsed, can you please suggest a way to do so, because data is filled by someone so using =LEFT(I3,FIND("hr",I3)-1) value does not work always as "hr" could be written as "Hr", "Hrs" or "hours". Need your help on this one.

    For Example
    1 hr 30 min should come out as 1.5 hrs
    24 hrs should come out 24 hrs
    17 hrs 20 mins should come out as 17.3 hrs

  16. How can I use the string of number position to return #name#
    e.g TF220721054112, if the 8th position read as '1' then the result to returned as #IMPORT#.
    If the 8th position read as '4' then the value returned as #Structure#

    Appreciate your advice.

  17. Need Result Like
    one cell value 9871541 and second cell only 9852 so i want match second cell value to first
    if second cell starting digit match with first cell starting digit then give value or highlight that cell

  18. Hi, How to extract number from combination of 4".
    Thanks
    Sandeep

    • Hi!
      Have you tried the ways described in this blog post? Pay attention to the second paragraph: How to extract number from the beginning of text string.

  19. Hi I need to extract Roman number from below text

    School Package + Live Classes Yearly,IX(1) | School Package + Live Classes Yearly,X(1) | School Package + Live Classes Yearly,VII(1) | School Package + Live Classes Yearly,VIII(1)

    Result:- IX,X,VII,VIII

  20. Hi i need to extract number from below text
    -p00881-shahwar taskeen-2-d-5/4 nazimabad no 2 -0322-2929699 : 021-36684652-1-blvrd crnr-precinct-38-street 04 & link road 04

    Number i wanna generate the number is 0322-2929699

    What will be the formula

    • Hi!
      Your data does not contain any template by which you can determine the desired section of text. If such a pattern exists, please describe it.
      To extract part of the text, you can use the MID function.

      =MID(A1,49,13)

      But I don't think that this will work for all data.

      • Can u guide me how to sort this data

Post a comment



Thank you for your comment!
When posting a question, please be very clear and concise. This will help us provide a quick and relevant solution to
your query. We cannot guarantee that we will answer every question, but we'll do our best :)