Function Problems

How to Extract and Trim Numeric Codes with Excel MID Function

Aamir Naveed AkramAamir Naveed Akram Nov 11, 2026 868 views

Question details

The user needs to extract a numeric code from a specific position in a text string using the MID function, while properly handling leading or trailing zeroes to return clean numbers or blanks.

How to Extract and Trim Numeric Codes with Excel MID
Product
Excel / WPS Spreadsheet
Device & OS
not provided
Scenario
Extracting substring numbers from a larger text string where extracted values might have unwanted zeroes (e.g., converting '0934' to 934, '0004' to 4, or handling entirely blank/zero results).
Observed behavior
Using a basic MID function returns text with leading/trailing zeroes. The formula needs to output pure numeric values or a blank cell if the extracted result resolves to zero.
Before you start

Identify the exact starting position and length of the numeric code in your text string, and determine whether you need the final result to be formatted as a mathematical number or left as a text string.

Solution 1Recommended

Use the LET Function and Double Unary (--)

Convert extracted text strings into pure numeric values using the double unary operator, and automatically output blanks for zero values.

1
Select the destination cell

Click on the cell where you want the extracted number to appear.

2
Enter the LET formula

Type the formula: =LET(a,--MID(A1,40,4),IF(a=0,"",a)) into the formula bar. Replace A1 with your target cell, 40 with the starting character position, and 4 with the length of the string to extract.

3
Apply and drag

Press Enter to calculate the result. The double minus (--) forces the text into a number, which automatically strips leading zeroes (e.g., '0205' becomes 205). Drag the fill handle down to apply the formula to the rest of your column.

Use the LET Function and Double Unary (--)
Handling Zeroes: The IF(a=0,"",a) portion of the formula ensures that if the extracted portion is entirely zeroes, the cell will return a blank instead of displaying a 0.
Powerful Spreadsheet Alternative

Easily Extract Text and Numbers with WPS Spreadsheet

WPS Spreadsheet fully supports advanced text extraction functions like MID, LET, and SUBSTITUTE, ensuring seamless compatibility with Microsoft Excel workbooks. You can clean, extract, and analyze your data efficiently.

  1. 1. Open your file in WPS Spreadsheet: Launch WPS Office and open your spreadsheet containing the text strings.
  2. 2. Enter the extraction formula: Click the target cell and enter the exact same =LET(a,--MID(A1,40,4),IF(a=0,"",a)) formula.
  3. 3. Apply to the column: Press Enter, then click and drag the small square at the bottom right of the cell to apply the extraction logic to your remaining data.
Fully compatible with Microsoft Excel file formats (.xlsx)Supports advanced dynamic array functions and LET for complex data extractionLightweight and fast, ideal for handling large datasetsFree to use with a familiar user interface
microsoft office alternative - wps office

Frequently Asked Questions

Why does the MID function return numbers as text?

The MID function is classified as a text function in spreadsheet software. It always returns a text string, even if the extracted characters are digits. To use them in calculations, you must convert them using mathematical operations like multiplying by 1, using the double unary (--), or applying the VALUE function.

How do I keep leading zeroes when extracting numbers?

If you need to keep leading zeroes (for example, keeping '0934' exactly as it is), simply use the standard MID function without converting it to a number. For example: =MID(A1,40,4) will keep the text format and preserve the leading zeroes.

What does the double minus (--) do in the formula?

The double minus, known as the double unary operator, is a fast and efficient way to coerce a text string containing numbers into a true numeric value. It functions essentially like multiplying the text by 1 or using the VALUE function, stripping leading zeroes in the process.

Can I use Flash Fill instead of formulas for extracting numbers?

Yes. In many cases, you can type the desired extracted number in the adjacent column and press Ctrl+E (Flash Fill). The spreadsheet will attempt to recognize the pattern and automatically extract the remaining numbers without needing a complex MID formula.