How-to recipes · Text
How to extract numbers from text in Excel
Pull numbers out of text like "Qty: 12 boxes" with REGEXEXTRACT or a SEQUENCE formula in Microsoft 365, and with MID, FIND and LOOKUP in older Excel.
Data copied from emails, web pages and old systems often mixes numbers with words: Order 4521, Qty: 12 boxes, Weight 2.5 kg. You cannot sum or sort such cells as numbers until the digits are separated from the text. If the numbers always sit in the same place, simple LEFT/RIGHT formulas do it. When they move around, you need a more general approach. Microsoft 365 now has regular expressions for this; older versions rely on some clever but well-tested formulas.
The example data
Four texts in A2:A5. The numbers are in different positions, one has digits on both sides of a letter, and one is a decimal.
Microsoft 365: REGEXEXTRACT
REGEXEXTRACT returns the first part of a text that matches a pattern. The pattern [0-9]+ means one or more digits:
| A | B | C | |
|---|---|---|---|
| 1 | Text | ||
| 2 | Order 4521 | ||
| 3 | Qty: 12 boxes | 12 | |
| 4 | Room 3B-17 | ||
| 5 | Weight 2.5 kg |
REGEXEXTRACT returns the text "12"; the double minus turns it into the number 12.
Result: 12
Useful patterns:
| Pattern | Finds | Example result |
|---|---|---|
[0-9]+ | the first run of digits | 4521 from Order 4521 |
[0-9]+(\.[0-9]+)? | a whole or decimal number | 2.5 from Weight 2.5 kg |
-?[0-9]+ | a number that may be negative | -15 from Balance -15 |
[0-9]+ with return_mode 1 | all runs of digits, spilled | 3 and 17 from Room 3B-17 |
REGEXEXTRACT is in current Microsoft 365 builds only. If your Microsoft 365 does not have it yet, use the next method.
Microsoft 365 (older builds) and Excel 2021: keep only the digits
This formula splits the text into single characters with SEQUENCE and MID, keeps those that are digits, and joins them with TEXTJOIN:
| A | B | C | |
|---|---|---|---|
| 1 | Text | ||
| 2 | Order 4521 | ||
| 3 | Qty: 12 boxes | ||
| 4 | Room 3B-17 | 317 | |
| 5 | Weight 2.5 kg |
Room 3B-17 becomes the text 317: all digits joined together.
Result: 317
SEQUENCE(LEN(A4))produces the positions 1, 2, 3 ... up to the length of the text.MID(A4, positions, 1)returns every character separately.*1turns digits into numbers and letters into#VALUE!; IFERROR replaces the errors with empty text.TEXTJOIN("",TRUE, ...)glues the remaining digits together.
-- to get a number.Excel 2016 and 2019: number at the end of the text
When the number is always at the end, find the position of the first digit and take everything from there with RIGHT:
| A | B | C | |
|---|---|---|---|
| 1 | Text | ||
| 2 | Order 4521 | 4521 | |
| 3 | Qty: 12 boxes | ||
| 4 | Room 3B-17 | ||
| 5 | Weight 2.5 kg |
The first digit of Order 4521 is at position 7, so RIGHT takes the last 4 characters.
Result: 4521
FIND({0,1,...,9}, A2&"0123456789")looks for each digit. Appending all ten digits guarantees that every FIND succeeds, so there is no error.- MIN returns the smallest position, which is where the first digit is.
LEN(A2)-position+1is the number of characters from there to the end, and RIGHT returns them.--converts the text to a number.
The same MIN(FIND(...)) part combined with MID works when the number is in the middle and followed by a space: =--MID(A3,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A3&"0123456789")),FIND(" ",A3&" ",MIN(FIND({0,1,2,3,4,5,6,7,8,9},A3&"0123456789")))-MIN(FIND({0,1,2,3,4,5,6,7,8,9},A3&"0123456789"))) returns 12 from Qty: 12 boxes. It is long; a helper column holding the MIN(FIND(...)) position makes it far easier to read.
No formulas: Flash Fill
Type the number from the first row by hand in the next column, then press Ctrl+E. Flash Fill often recognises the digits after Order or the number before kg. Check the results, and convert them to numbers (they may come out as text).
Pitfalls
- Text, not numbers. REGEXEXTRACT, MID, RIGHT and TEXTJOIN all return text. Use
--,*1or VALUE before summing; otherwise SUM ignores the results. - Decimal separators. In regional settings where the decimal separator is a comma, "2.5" does not convert with
--. Use NUMBERVALUE with the right separator. - Leading zeros disappear when converting to a number (
0042becomes 42). Keep them as text if they are part of a code. - Power Query is a better tool when you clean the same kind of file every week: Split Column > By Non-Digit to Digit. See Power Query basics: import, clean, append, merge, refresh.