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
hello i would like guidance on how to extract certain data from collumns
there are just number & mixed with multiple lines
Example :
Cells Value
[ B2 ] 100
[ B3 ] Phone:3050
Cards:91
Banks1:33.52
[ B4 ] 500
[ B5 ] 600
I would like to get the number extracted on the next row so it only show number :
Cells Value
[ B2 ] 100
[ B3 ] 3050
[ B4 ] 500
[ B5 ] 600
Hello! To extract the first number from the text, you can use the user-defined function RegExpExtract. You can find the examples and detailed instructions here: How to extract substrings in Excel using regular expressions (Regex).
For example:
=RegExpExtract(A1, "\d+", 1)
I recommend paying attention to the Regex tool. You can find, extract, compare, delete, or replace strings that match the regular expression pattern you enter. It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
Hi,
How can I extract the number that does not contain any text?
Eg:
P2_G1038_E_29.docx
It will extract 29
P2_G2151_H_05.docx
It will extract 05
P2_R7078_H_03_xxxx_xxx_xxxxxxxxx_control_after_printing.pptx
It will extract 03
P2_TW3429_H_Z8F78061234.pptx
It will extract nothing
Thanks & regards,
Nurul
Hello! Use regular expressions and the custom function RegExpExtract to search for pattern characters in your text. For detailed instructions and examples, see here: Regex to extract strings in Excel (one or all matches).
=MID(RegExpExtract(A1, "_([0-9]{1,3})"),2,10)
To avoid installing VBA code in your workbook, you can use the Regex Tools. It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
Hi i need to extract 4700090584/4700083771 number from below text
BH-24&4700090584ARC FOR MONSOON PREPARATION & OTHE
4700083771/JAMADOBA COLLIERY/RC96
What will be the formula?
Thanks & Regards
Hi! To extract groups of numbers from text by pattern, use regular expressions. We have a special tutorial on this. Please see Regex to extract strings in Excel (one or all matches).
Use this formula:
=TEXTJOIN("/",,RegExpExtract(A1, "\d{3,15}"))
The TEXTJOIN function will merge all the extracted numbers into a test string.
Dear Sir,
From this text UTR,BKIDY23129107772,BANK OF INDIA, I want "BKIDY23129107772" this Alphanumerical separate in next column.
Thanks & Regards
Pawan
Hi! We have a special tutorial on this. Please see: How to split text string in Excel by comma, space, character or mask. Try formula:
=MID(A2, SEARCH(",",A2) + 1, SEARCH(",",A2,SEARCH(",",A2)+1) - SEARCH(",",A2) - 1)
You can also use the TEXTSPLIT function to split the text into cells.
=TEXTSPLIT(A2,",")
Thank you very much for the formulas, i need to extract exact numbers from string and the text contains as well other numbers. Can you please show me how to solve this?
for example: Lnd/lnd rigts/bldgs, on 3rd-prty lnd
12345678
I need to get exact number "12345678"
After applying formula: SUMPRODUCT(MID(0&C7, LARGE(INDEX(ISNUMBER(--MID(C7, ROW(INDIRECT("1:"&LEN(C7))), 1)) * ROW(INDIRECT("1:"&LEN(C7))), 0), ROW(INDIRECT("1:"&LEN(C7))))+1, 1) * 10^ROW(INDIRECT("1:"&LEN(C7)))/10)
The result is: 312345678
Thanks so much in advance!
Hi! You can extract numbers from a pattern using the custom REGEX function. For example,
=RegExpExtract(A1, "\d{8}", 1)
You can find the examples and detailed instructions here: How to extract substrings in Excel using regular expressions (Regex). I recommend paying attention to the Regex tool. You can find, extract, compare, delete, or replace strings that match the regular expression pattern you enter. It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
Thank you SO MUCH for the formula for extracting numbers from a string. It works perfectly :)
Thank you SO MUCH for the formula for extracting numbers from a string. It works perfectly :)
Hi there! I want to extract a UPC 12 digit number from below description in excel colomn. How i do this? Your cooperation in this regard is highly appreciable. Thanks!
"HBG86-4 Hot Wheels Make A Mactch Card Game (4 per case) UPC 887061988741 Asin B08V54CNWTt's the classic card matching that kids love made into a game with graphics themed to another childhood playtime staple – Hot Wheels cars!This game is designed"
Hi!
You can extract the numeric code from the text with the custom function RegExpExtract. You can see detailed instructions and an example file in this article: How to extract substrings in Excel using regular expressions (Regex).
=MID(RegExpExtract(A1,"UPC ([0-9]{12})"),5,12)
You can also use Regex tools without formulas. It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
Thanks! Formula working. But i face a problem because sometimes UPC have 13 digits and and when i put formula =MID(RegExpExtract(A1,"UPC ([0-9]{12})"),5,12) only 12 digits of UPC are shown. How to solve this problem so that 12 digit UPC and 13 digit UPC are shown exactly as original. Thanks Again for your cooperation.
Hi!
The formula I sent to you was created based on the description you provided in your first request. However, as far as I can see from your second comment, your task is now different from the original one. Add a choice of 12 or 13 digits to the formula. I recommend reading this guide: Excel Regex cheat sheet.
=MID(RegExpExtract(A1,"UPC ([0-9]{12,13})"),5,20)
Hi Alexander,
I need your support on how to extract the pack-size from the text string.
Example: XXLR SHAMP ANT-HR FALL (NPS)GF 24X200ML
I just need 200 ML as a result. The position of the pack-size within the text string differs for every description thus can't use the formula =Left (A2, 5) .
I would appreciate your help in this regard.
Thanks!
Hi!
In your example you can use the RIGHT function. If your data has some common pattern, you can use the custom function RegExpExtract to extract the pack-size. See this manual for detailed instructions and examples: How to extract substrings in Excel using regular expressions (Regex).
I recommend paying attention to Regex Tools. You can use it to find, extract, delete, or replace strings that match the pattern you entered.
It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
If you give more examples of the source data and the desired result, I will try to help.
Hi, can you help me to extract specific number from string of numbers in a row, eg
0 0 1 0 3 0 4 0 0 6 7 9 0 0 0 - how to extract number 4 from this string of numbers....Tq
Hi!
To extract a specific character from a middle of text string, you can use MID function.
Try this formula:
=MID(A1,13,1)
Thanq Very Much.
Hi, Could i extract the number of Net Outage days, hours and mins from the following text, and add them to seperate columns
"Clock Stops - 0days 4hours 15mins Net Outage - 0days 0hours 53mins Escalated"
Hello!
You can extract each number to a separate cell with the custom function RegExpExtract. See this manual for more information and examples: How to extract substrings in Excel using regular expressions (Regex).
=RegExpExtract(A1, "\d+")
Also you can use Regex Tools. It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
Thank you. Ill give it a go
How would I extract only the largest number from a string including double digit numbers.
Ex (adcd)4(abcd)12(abcd)20(abcd)15
And I need just the 20 extracted
Hello!
You can extract substrings matching a given pattern using regular expressions. Use custom function RegExpExtract, as described in this guide: How to extract substrings in Excel using regular expressions (Regex).
You can extract the maximum number from the text using the following formula:
=MAX(--RegExpExtract(A1,"\d+"))
With Regex Tools, you can find, extract, remove, or replace strings that match a regex pattern you enter.
It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
How would I get it to take all of the numbers, even if it starts with zeros? For example, I need it to pull 002, but when I use the formula above, it only pulls "2". How can I make sure that the two zeros are included? Thank you!
Hello!
The answer to your question can be found in this article: Leading zeros in Excel: how to add, remove and hide.
212LBTCE94995226 I WANT TO EXTRACT NUMBER FROM THIS TEXT
Hi!
Please re-check the article above since it covers your task.
Hello, how would I go about extracting only the 197, 57, and 166 (the dimensions) from the following cell? Ideally each extracted number should be in a separate column.
Package 1 — L: 197cm, W: 57cm, H: 166cm, Weight: 75kg, qty: 1
PS: the formula should be copied down do other cells which have different numbers
Hello! To extract multiple numbers from a text string, use regular expressions and the custom function RegExpExtract. You can find the examples and detailed instructions here: How to extract substrings in Excel using regular expressions (Regex).
To extract not all numbers from the text, extract the desired part of the text string using this instruction: How to extract text between two characters in Excel.
Try this formula:
=TRANSPOSE(RegExpExtract(MID(A1, SEARCH("—", A1) + 1, SEARCH("Weight", A1) - SEARCH("—", A1) - 1),"\d+"))
With Regex Tools, that are part of Ultimate Suite for Excel, you can find, extract, remove, or replace strings that match a regex pattern you enter. It is available as a part of our Ultimate Suite for Excel that you can install in a trial mode and check how it works for free.
Thanks for the answer. I forgot to mention that I am doing the analysis on Google Sheets (so no support for Regex). Would you have any other ideas?
Hello Lorenzo,
REGEXEXTRACT also works in Google Sheets. Please read this tutorial to extract numbers in Google Sheets.
Hi
How can i extract the first number (with 4 or 5 digits) from rows like these two:
69002-ODN3-S-RFI-0197
7082-E-RFI-0038
I can also experience a randow row looking like this where i still want the first 4 digit number:
#7082-E-RFI-0071
and at the same time get no value when the rows look like these for example:
XXX-ODN3-960
ODN3-BIM-RFI-0014
E&I-SWBD-RFI-057
Hello!
Please check the formula below, it should work for you:
=CONCAT(IFERROR(MID(REPLACE(A1,IFERROR(FIND("-",A1,1),1),100,""),ROW(1:10),1)*1,""))
This does not work at all. I tried with their exact example 05-EC-01 and formula and it returned #N/A. So frustrated I have been wasting my with this
Hi!
What formula did you use and what numbers did you want to extract?
Thank you for the information it has been very helpful to pull the numbers I need out of a text. Now i want to just delete the numbers I pulled so i can use the data for lookup.
=LOOKUP(9.9E+307,--RIGHT(MID(A20,MIN(FIND({1,2,3,4,5,6,7,8,9,0}, $A20&"1023456789")),999),ROW(INDIRECT("1:999"))))
This worked to get what I needed.
1ST SHIFT OUTBOUND NON CON LIFT OPERATOR PTO 8.00
1ST SHIFT OUTBOUND NON CON LIFT OPERATOR HOLIDAY 8.00
1ST SHIFT OUTBOUND NON CON LIFT OPERATOR REGULAR 55.75
1ST SHIFT OPERATIONS INVENTORY CONTROL CYCLE COUNTER OVERTIME 0.50
1ST SHIFT OPERATIONS INVENTORY CONTROL CYCLE COUNTER REGULAR 40.00
1ST SHIFT MAINTENANCE MAINTENANCE 2 SANITATION WORKER OVERTIME 0.50
1ST SHIFT MAINTENANCE MAINTENANCE 2 SANITATION WORKER REGULAR 40.00
1ST SHIFT MAINTENANCE MAINTENANCE 1 MAINTENANCE MECHANIC REGULAR 64.00
Now I want to delete the numbers at the end.
Hi!
Unfortunately, your formula does not work for me. To remove numbers from text try this formula -
=SUBSTITUTE((CONCAT(IF(NOT(ISNUMBER(--MID(A1,ROW($1:$95),1))),MID(A1,ROW($1:$95),1),""))),".","")
That works to remove all the numbers but I only wanted to remove the numbers on the right.
Hi!
To remove the last word from a text string, use this formula:
=TRIM(REPLACE(SUBSTITUTE(A1," ",REPT(" ",20)), LEN(SUBSTITUTE(A1," ", REPT(" ",20)))-19,20,""))
Thank you that worked perfect. I really do appreciate your help.
Hi,
How to extract mobile number alone from the below text string
"ABC Imaging & Diagnostics,
80-5/4-3, TH Road,
Chennai-520000,
TamilNadu, India
E-mail: abcimaging @ yahoo.co.in
Contact Person: ABC
Mobile: 9009949999"
Hi!
Search for the word "Mobile:" using the SEARCH function and extract all subsequent characters.
Try this formula:
=MID(A1,SEARCH("Mobile",A1)+8,20)
I've been racking my brain on how to find numbers before Certain text and this helped a lot thank you very much!!
=MID(C3,SEARCH("GA",C3)-3,2)
Was the Formula I used.
How to put comma (,) before any number in the address formal? Numbers are not fix in the address it might be on any position.
Hi!
To understand what you want to do, give an example of the source data and the desired result.