Skip to content

How to extract numbers from text in Excel in Excel

Pull a number out of a text string such as INV-10452 or Box 12 kg using TEXTAFTER, TEXTBEFORE and VALUE, or by keeping only the digits.

A reference like INV-10452 or a label like Crate 140 kg has a number inside it. You want the number on its own, as a real number you can add up.

The key is that text functions return text, even when the text looks like a number. =TEXTAFTER(A2,"-") returns "10452" as text, which will not sum or sort as a number. Wrap the result in VALUE, or put -- in front of it, to convert it.

When the number sits next to a predictable separator, cut it out with TEXTAFTER and TEXTBEFORE. For Crate 140 kg, take the text after the first space, then the text before the next space: =VALUE(TEXTBEFORE(TEXTAFTER(D2," ")," ")).

When the digits could be anywhere, test each character. MID(D2, SEQUENCE(LEN(D2)), 1) splits the text into single characters, multiplying each by 1 turns digits into numbers and leaves errors for everything else, IFERROR blanks those, and TEXTJOIN glues the digits back together. This keeps every digit in the string, so it suits text that holds one number. If a string holds two numbers they run together.

The ribbon alternative for a fixed pattern is Flash Fill: type the number you expect beside the first row, then choose Data > Flash Fill or press Ctrl+E. It writes fixed values, so it will not update if the text changes.

Convert the results with VALUE even if they look right. A number stored as text is the most common reason a total comes out too low. The Logic and text track practises these conversions.

3 ways to do it

1TEXTAFTER and VALUE (predictable separator)

=VALUE(TEXTAFTER(A2,"-"))

  1. Find the character that sits right before the number, such as the hyphen in INV-10452.
  2. Type =VALUE(TEXTAFTER(A2,"-")) and press Enter.
  3. Fill down. VALUE turns the text into a number you can add up.

2Keep only the digits

=VALUE(TEXTJOIN("",TRUE,IFERROR(MID(D2,SEQUENCE(LEN(D2)),1)*1,"")))

  1. Split the text into characters with MID and SEQUENCE.
  2. Multiply each character by 1. Digits become numbers; everything else becomes an error, which IFERROR turns into an empty string.
  3. Join the digits with TEXTJOIN and convert with VALUE.

3Flash Fill (fixed values)

  1. Type the number you expect next to the first row.
  2. Choose Data > Flash Fill, or press Ctrl+E, and check the results.

Try it yourself

Solve it in the sheet. Every answer is checked, with hints if you need them.

Task 1 of 2

In B2, return the invoice number from A2 as a real number, then fill down to B8.

Answer in B2, then it fills down to B8.

B2
A
B
C
D
E
F
1
Reference
Number
Shipment
Weight (kg)
2
INV-10452
Box 12 kg
3
INV-10477
Crate 140 kg
4
INV-10513
Bag 5 kg
5
INV-10601
Drum 205 kg
6
INV-10688
Carton 38 kg
7
INV-10702
8
INV-10755

Learn it properly, one graded sheet at a time.

Logic and text covers this in lesson 13, TEXTBEFORE and TEXTAFTER.