Text

Excel formula to extract numbers from text

This formula looks at every character, keeps the digits and joins them back together into a number.

Excel 365 / Excel 2021+; Google Sheets

The formula

Excel 365Excel
=--TEXTJOIN("",TRUE,IFERROR(--MID(A2,SEQUENCE(LEN(A2)),1),""))
In C2 → 4821
Excel 365 (keep leading zeros as text)Excel
=TEXTJOIN("",TRUE,IFERROR(--MID(A3,SEQUENCE(LEN(A3)),1),""))
In C3 → 0075
Google SheetsGoogle Sheets
=VALUE(REGEXREPLACE(A2,"[^0-9]",""))
In C4 → 4821
Excel 365 with regexExcel 365
=--REGEXEXTRACT(A2,"[0-9]+")
In C5 → 4821
✓ Tested. All 2 Excel results in this example were recalculated in LibreOffice Calc 25.2 and matched the values shown. 2 results for formulas tagged Excel 365 / Google Sheets were checked with a reference implementation of the documented behavior, because LibreOffice can't run those functions.

Worked example

Paste the data below into a blank sheet starting at cell A1 (or download the cheat sheet, where every example is set up). Highlighted cells hold the formulas and show the expected results.

ABC
1Text
2Order #4821-B4821
3SKU AB-00750075
44821
54821

The TEXTJOIN/MID/SEQUENCE versions were verified in LibreOffice Calc. REGEXEXTRACT/REGEXREPLACE results were checked with a reference regex implementation.

How it works

SEQUENCE(LEN(A2))
1, 2, 3 … up to the length of the text, one position per character.
MID(A2,…,1)
Splits the text into single characters.
--MID(…)
Converts each character to a number; letters and symbols become #VALUE!.
IFERROR(…,"")
Turns those errors into empty text.
TEXTJOIN("",TRUE,…)
Joins the remaining digits. The leading -- turns "4821" into the number 4821.

Common errors and fixes

All digits run together?

"Size 10 x 20" returns 1020. Use REGEXEXTRACT(A2,"[0-9]+",1) in Excel 365 to get each number separately.

Decimals lost?

The dot isn't a digit, so 12.5 becomes 125. Use =--REGEXEXTRACT(A2,"[0-9]+\.?[0-9]*") in Excel 365 or REGEXEXTRACT in Sheets.

Why does the formula return #VALUE!?

The cell has no digits at all, so -- gets an empty string. Wrap in IFERROR(…,"").

Does this work in Google Sheets?

Yes. Use the formulas tagged "Google Sheets" on this page; formulas tagged only "Excel" use Excel-specific syntax.