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.
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:
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, "")
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:
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:
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 :)
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):
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:
- Go to the Ablebits Data tab > Text group, and click Extract:
- Select all cells with the source strings.
- On the Extract tool's pane, select the Extract numbers radio button.
- 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.
- Click the Insert Results button. Done!
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:
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)
565 comments
I have a cell with a string of ISBN numbers separated by a bar. I am trying to extract the first number that contains 13 digits and begins with 978.
0008234159|0008234167|0062678418|0062791451|006279955X|9780008234157|9780008234164|9780062678416|9780062791450|9780062799555
The above numbers are in one cell. I need the first one that starts with 978 and contains 13 digits. I can easily do a right function with there are only two and the second is the one I need. Many times this doesn't work.
How to separate the text and no. from string like this:
Where hand power only is used 97.30 166
Where any mechanically driven machinery is used 121.60 761
Fish Dehydrating - 24.30 717
Excl. erection, decoration tents and mosques 12.15 55 167
Incl. Erection 24.30 168
Floorcloth Linoleum Mfgrs. 30.15 725
Flour and Dal Mills 15.10 169
hello cen somebody please write me code for extract last 4 digits (0470) before P in serila number 1908910470P46363902R77391
Hi Aljaž,
Try this formula, where A2 is the serial number:
=MID(A2, SEARCH("p",A2) - 4, 4)
Hi Svetlana,
Thanks for the tip but when you write a formula to my table, it return error.
Any suggestions?
Ok, I found problem. , needs to be swich for ; and then it works correctly.
In a excel i have to add numbers from 2 different cells, 1st cell to have number before the decimal point and the other cell to have numbers after the decimal point.
however both cells are linked to sheet 1 from which the cell number changes upon the entry, For example
First Cell would be on D6 which is 10.25
Second cell would be on O6 which is 6.77
but the answer in cell E6 would be 10.77
Can anyone help me with this please.
hi
i have 234598 number
i want per number put on a cell
234598 2 3 4 5 9 8
I have serial number in a cell like 12345678910 and i want to some this in a different cell so how can i do this?
plse help me 8127701024 this is my whatsapp number so pls help me
i need to multiply:
40,000 sq.ft of gross building area @25.00/sq.ft
Hi I need some help!
I have cells that include an inconsistent title but all include a 6-digit meeting code. is and some of the inconsistent titles have numbers before the 6-digit code. Any thoughts?
What I have:
146761 - Trane Extended Leadership Meeting - April 2015
RFPIGR19 - 161291 - Society of Women Engineers 2019 National Conference - November 2017
RFPIGR19-161791-IWD Q1-QOR Reviews
What I need:
146761
161291
161791
DATA
(A) 10400 OFF WHITE (B) 23100 CAMEL (C) 23100 CAMEL + 10400 WHITE
NEED OUT PUT
10400,23100,23100,10400
Please help me !
Anyone please,
How can I extract the qty/value which is between texts. (e.g. 260 Individual Removal of Trees,small 303.00 each) I need to extract the number 303 only.) Thanks.
Lb29235John033921058 this is in A1
need formula for only "33921058"
need just the last set of numbers but sometimes the count can vary in length so ex...
last #s in a2 = 3 numbers
last #s in a3= 5 numbers
last #s in a4 = 9 numbers and so on
AMANACDEDITA FLOW BACK 18.85G abcde
RWK MIX DN/LAKA 4DSX12UNX135G
These are two values in cells which i need value of "G" means number which are together with "G"
Exa -1 = Input - AMANACDEDITA FLOW BACK 18.85G abcde so output would be - 18.85G because 18.85 is number with "G"
Exa -2 = Input - RWK MIX DN/LAKA 4DSX12UNX135G so output would be - 135G because 135 is number with "G"
Please Help.
thanks
Hello!
"H004.16K6170" I need to get the number out of text include 0(left and right). for example "004166170"
How can I do it? please help ;-;
Hi,
I have a 16 bit binary number in one cell, and have to read only 2 bit data from 5th position.Any excel formula for this?
For Ex : 0100100001010011 is my bin number
5th position counting from right is the 6th place(O,1,2,3,4,5)
So it is 0, taking 2 bits to right is 01
The answer is 01
I have this column, where ALL is specified in other cells eg H1=270 and H2=9300
ALL
ALL
8412
319-3234
8538-8542
356
2665
And i would like to create this:
A B
270 9300
270 9300
319 3234
8538 8542
356 356
2665 2665
Hellow
I would like to extract the number right before "days" from following an example. Could you help me?
60 years, female, white, stage:iia, alive, 588 days
female, asian, stage:iib, alive, 2759 days
80 years, female, black or african american, stage:iia, alive, 3364 days
I need
588
2759
3364
Thanks,
Very helpful solved the problem.
Hi, I have data like....agjkkhffjkkhfcnkkhfc2445543345fhhffh,
I need only numbers like 244554334 pls help with formulas.
Hi! I have "update" data in cells, like below (xxxx - string):
181215 xxxxx xxxxx xxxx xxxx 34xx 3-4xxxx 181216 2xxxx-xx xxxx xx3 xxx, etc. where 181215 and 181216 are dates.
I'd like to extract the last date (here it is 181216) from the string with functions (not with macro).
Could you help me with this? Much appreciated! Thanks!
PS: In the tutorial there's a little error at: =RIGHT(B2, LEN(A2)-B2+1). The working formula is =RIGHT(A2, LEN(A2)-B2+1) like under it.
Thank you so much for this incredibly helpful article. The formulas almostttt work for me, but I am hoping to only retrieve the last 4 characters from a column entry ONLY in the case that the last four characters are NUMERIC, and even if there are numerals/text earlier in the string. My column entries are all different lengths so I am not able to use text to columns, either. Here is an example: If my column entry were to say "Blue Sky 1st Bracket 1999", I would only like to get the "1999" in its own separate column (without the inclusion of the first '1' earlier in the string). If my second column entry were to say "Blue Sky", I would like nothing to appear in the separate column. Thank you so much for your help.