Excel Formula to Extract Numbers from Text
In this article
You can extract numbers from text in Excel with a formula built from TEXTJOIN, MID, ROW and INDIRECT, entered as an array formula in older versions or as a normal formula in Microsoft 365. The trick is to test each character of the cell, keep only the digits, and join them back into a single string. There is no single built-in function called EXTRACTNUMBERS, so the formula below is the standard approach that works without macros or add-ins.
What You'll Need
- Excel 2016 or later, or Microsoft 365. Excel 2013 and earlier need Ctrl+Shift+Enter for array formulas.
- A column of text values that contain the numbers you want, for example order codes like "INV-2045-A" or product labels like "Model 77B" .
- A blank column beside the data for the results, and a spare row number range (the formula uses ROW to count characters).
- Basic familiarity with entering formulas and dragging the fill handle.
Step-by-Step
- Put your source text in column A, starting at A2. Leave row 1 for headers so the formula has a clean reference point.
- Click cell B2 and type this formula exactly:
=TEXTJOIN("",TRUE,MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)*1) - Press Enter. In Microsoft 365 and Excel 2021 the formula spills and returns the digits as a single number. In Excel 2019 and earlier, press Ctrl+Shift+Enter instead so Excel treats it as an array formula; you will see curly braces around it.
- Check the result. For A2 containing "INV-2045-A", B2 should show 2045. For "Model 77B" it should show 77. If the cell has no digits, the formula returns an error, which is expected and covered in Troubleshooting.
- Drag the fill handle from B2 down to the last row of your data. Each row now pulls its own numbers.
- If you want the result stored as text rather than a number (useful for leading zeros), wrap the whole formula in TEXT or add an apostrophe when you paste values. Otherwise leave it as a number so you can sum or sort it.
- To keep only the values and drop the formula, select column B, copy it, then use Paste Special > Values over the same range. This is worth doing before you delete column A.
The formula works because MID pulls one character at a time, multiplying by 1 turns digits into numbers and everything else into an error, and TEXTJOIN with the ignore_empty argument set to TRUE skips those errors. That is why the joined result contains only digits. If your text has several separate number groups, for example "2 apples and 15 pears", the formula joins them into "215" rather than returning 2 and 15 separately. To get the first group only you need a different approach, such as Flash Fill or a helper column per group.
Common Mistakes
- Forgetting Ctrl+Shift+Enter in older Excel. Without it the formula returns only the first character's result and looks broken.
- Using a fixed range like ROW(1:100) when the text is longer than 100 characters. The formula then misses trailing digits. Use LEN(A2) as shown so the range always matches the cell length.
- Pointing INDIRECT at a closed workbook. INDIRECT fails on closed files, so keep the source workbook open while the formula calculates.
- Assuming decimals survive. The multiplication step treats the decimal point as a non-digit and drops it, so "12.5 kg" becomes 125. If decimals matter, use a different method or clean the data first.
- Leaving the result as a formula and then deleting the source column. The formula then returns #REF!. Paste values first.
- Expecting the formula to handle negative signs. The minus sign is not a digit, so "-40" returns 40. Add the sign back manually if needed.
Tips
- For a quick one-off job, use Data > Flash Fill after typing the first expected result in the adjacent column. It is faster than the array formula and handles decimals and signs better, but it does not update when the source changes.
- If you have Excel 365, the same logic works with TEXTSPLIT and TEXTJOIN in a single formula, which is easier to read and does not need INDIRECT.
- Keep a helper column with LEN(A2) if you want to audit why a particular row returned an unexpected value.
- Use TRIM on the source cell first to remove stray spaces that can confuse the character count.
- Test on a copy of the sheet before applying the formula to thousands of rows, since array formulas on large ranges can slow recalculation.
Troubleshooting
If the formula returns #VALUE!, the cell contains no digits at all. Wrap it in IFERROR to show a blank instead: =IFERROR(TEXTJOIN("",TRUE,MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)*1),"").
If it returns only the first digit, you almost certainly missed Ctrl+Shift+Enter on an older version. Re-enter the formula and confirm the curly braces appear in the formula bar.
If it returns a wrong number, check the source cell for hidden characters, non-breaking spaces or line breaks. Paste the cell into Notepad first to see what is really there, then clean it with SUBSTITUTE or TRIM.
If the result is text that will not sum, the multiplication step may have been removed. Confirm the *1 is still present before the closing parenthesis of MID.
If recalculation is very slow, limit the formula to the used range rather than entire columns, and paste values once the results are correct.
Frequently Asked Questions
Is there a built-in Excel function that extracts numbers from text?
No. Excel has no dedicated extract-numbers function. The TEXTJOIN and MID combination above is the standard formula method, and Flash Fill is the quick manual alternative.
Does the formula work in Excel for Mac?
Yes. The functions are the same. On older Mac versions use Command+Shift+Enter for array entry instead of Ctrl+Shift+Enter.
Can it extract numbers from multiple cells at once?
It works per cell. Enter it in the first result cell and drag down. In Microsoft 365 you can point it at a range and let it spill, but each row still needs its own LEN reference.
Why does it return 125 instead of 12.5?
The decimal point is not a digit, so it is discarded. Clean decimals with a different formula or use Flash Fill if decimals must be preserved.
Will it pick up numbers written as words, like "two"?
No. It only keeps digit characters. Spelled-out numbers need manual conversion or a lookup table.
Can I use this in Google Sheets?
The same TEXTJOIN, MID, ROW and INDIRECT pattern works in Google Sheets and is entered as a normal formula. Results may differ slightly on very long strings, so test a few rows first.