The formula
=--TEXTJOIN("",TRUE,IFERROR(--MID(A2,SEQUENCE(LEN(A2)),1),""))=TEXTJOIN("",TRUE,IFERROR(--MID(A3,SEQUENCE(LEN(A3)),1),""))=VALUE(REGEXREPLACE(A2,"[^0-9]",""))=--REGEXEXTRACT(A2,"[0-9]+")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.
| A | B | C | |
|---|---|---|---|
| 1 | Text | ||
| 2 | Order #4821-B | 4821 | |
| 3 | SKU AB-0075 | 0075 | |
| 4 | 4821 | ||
| 5 | 4821 |
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.