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.

Advanced REGEXEXTRACT TEXTJOIN MID SEQUENCE LEN IFERROR RIGHT FIND MIN VALUE

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:

C3fx=--REGEXEXTRACT(A3,"[0-9]+")
ABC
1Text
2Order 4521
3Qty: 12 boxes12
4Room 3B-17
5Weight 2.5 kg

REGEXEXTRACT returns the text "12"; the double minus turns it into the number 12.

Result: 12

Useful patterns:

PatternFindsExample result
[0-9]+the first run of digits4521 from Order 4521
[0-9]+(\.[0-9]+)?a whole or decimal number2.5 from Weight 2.5 kg
-?[0-9]+a number that may be negative-15 from Balance -15
[0-9]+ with return_mode 1all runs of digits, spilled3 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:

C4fx=TEXTJOIN("",TRUE,IFERROR(MID(A4,SEQUENCE(LEN(A4)),1)*1,""))
ABC
1Text
2Order 4521
3Qty: 12 boxes
4Room 3B-17317
5Weight 2.5 kg

Room 3B-17 becomes the text 317: all digits joined together.

Result: 317

  1. SEQUENCE(LEN(A4)) produces the positions 1, 2, 3 ... up to the length of the text.
  2. MID(A4, positions, 1) returns every character separately.
  3. *1 turns digits into numbers and letters into #VALUE!; IFERROR replaces the errors with empty text.
  4. TEXTJOIN("",TRUE, ...) glues the remaining digits together.
Watch out. This method joins all digits, so it only makes sense when the text contains a single number. Room 3B-17 becomes 317, which is probably not what you want, and 2.5 becomes 25 because the decimal point is dropped. Wrap the formula in -- 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:

C2fx=--RIGHT(A2,LEN(A2)-MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789"))+1)
ABC
1Text
2Order 45214521
3Qty: 12 boxes
4Room 3B-17
5Weight 2.5 kg

The first digit of Order 4521 is at position 7, so RIGHT takes the last 4 characters.

Result: 4521

  1. FIND({0,1,...,9}, A2&"0123456789") looks for each digit. Appending all ten digits guarantees that every FIND succeeds, so there is no error.
  2. MIN returns the smallest position, which is where the first digit is.
  3. LEN(A2)-position+1 is 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 --, *1 or 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 (0042 becomes 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.
Tip. If the number sits between two known characters, such as brackets or dashes, the simpler approach in How to extract text between two characters in Excel is easier to maintain.
⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 16 KB

More how-to recipes