logo
search
Function Problems

Excel Formula to Extract a Variable Number of Characters

Algirdas JasaitisAlgirdas Jasaitis Oct 16, 2026 870 views

Question details

The user needs to extract a specific number of characters starting at position 7 from a text string. The default length is four characters, but a significant number of exceptions require lengths of five or six characters. The user wants to avoid writing an extremely long nested IF statement.

How to Extract a Variable Number of Characters in Excel
Product
Excel
Device & OS
not provided
Scenario
Extracting varying substring lengths from a dataset where specific text codes dictate different character extraction requirements.
Observed behavior
Attempting to use nested IF functions for numerous exceptions creates formulas that are too long, difficult to maintain, and prone to errors.
Before you start

Before building your formula, compile a list of all your exception values and their corresponding required extraction lengths in a blank section of your worksheet.

Solution 1Recommended

Use a Lookup Table with MID and XLOOKUP

Replace a long nested IF statement with a clean XLOOKUP function that references a separate table of exception values, seamlessly nested inside a MID function.

Instead of hardcoding every exception directly into the formula, a lookup table keeps your data organized. By combining MID with XLOOKUP and IFERROR, you can look up the required length for any string. If the string is not an exception, the formula defaults to the standard length.

1
Set up the exceptions table

In a separate area of your worksheet, create two adjacent columns. List all exception text values in the first column and their required lengths (e.g., 5 or 6) in the second column.

2
Define named ranges

Highlight the column containing the exception text values and name the range 'Exceptions' in the Name Box. Next, highlight the lengths column and name it 'Lengths'.

3
Enter the formula

Click on the cell where you want the extracted text to appear. Type the formula: =MID(A2,7,IFERROR(XLOOKUP(A2,Exceptions,Lengths),4)) and press Enter.

4
Apply to the entire dataset

Select the cell containing your new formula, then click and drag the fill handle (the small square at the bottom-right corner) down to apply the extraction logic to all rows.

Use a Lookup Table with MID and XLOOKUP
Handling Partial Matches: If the exception rule is based on a specific code within the text rather than matching the entire cell value, you can use XLOOKUP with wildcard characters or add a separate code-extraction step.
Efficient Data Processing

Process Complex Formulas Easily with WPS Spreadsheet

WPS Spreadsheet fully supports advanced logical and text functions like XLOOKUP, MID, and IFERROR. You can easily set up lookup tables and extract variable characters without experiencing lag or performance issues.

  1. 1. Open your dataset in WPS Spreadsheet: Launch WPS Office, select Spreadsheet, and open the workbook containing the text strings you want to process.
  2. 2. Create an exception lookup table: Use empty columns to list your unique text codes and their corresponding variable extraction lengths.
  3. 3. Apply the MID and XLOOKUP formula: Enter the nested lookup formula into your target cell to instantly reference your table and extract the correct character count.
  4. 4. Drag to fill the column: Use the fill handle to copy the intelligent extraction formula down your entire data column.
Seamlessly handles advanced array and text formulas like XLOOKUP and MID.Fully compatible with Microsoft Excel (.xlsx) file formats.Lightweight architecture ensures rapid processing for massive datasets.Familiar, user-friendly interface requiring no learning curve.
microsoft office alternative - wps office

Frequently Asked Questions

Can I use VLOOKUP instead of XLOOKUP for this formula?

Yes. If you prefer or are using an older software version, you can substitute XLOOKUP with VLOOKUP combined with IFERROR. However, XLOOKUP is generally recommended because it requires fewer arguments and handles missing values more gracefully.

What if the extraction starting position also varies?

If your starting point is not fixed at position 7, you can replace the hardcoded '7' in the MID formula with a SEARCH or FIND function to dynamically locate a specific delimiter or character to start from.

How do I handle exceptions that require extracting text from the end of the string?

For characters at the end of a string, replace the MID function with the RIGHT function. You can still use the XLOOKUP logic to determine how many characters the RIGHT function should pull.

Why is my formula returning an error for standard values?

This happens if standard values aren't found in your exceptions table and your lookup function isn't properly wrapped in an IFERROR statement. Ensure the formula includes IFERROR(..., 4) so it defaults to 4 characters when a match is not found.