Function Problems

Extract a Three-Letter, Four-Digit Code from Excel Text

Natalie TaylorNatalie Taylor Nov 18, 2026 870 views

Question details

Extract a code containing three letters and four digits from text strings.

Extract a Three-Letter, Four-Digit Code from Excel Text
Product
Microsoft Excel
Device & OS
not provided
Scenario
The code appears between the final underscore and the following hyphen, and the source may contain up to one million rows.
Observed behavior
The desired result is the text between those delimiters, such as SAM3994 from Warkaa_SAM3994-2.
Before you start

Check that each source string has an underscore followed by a hyphen and that the code is between the final underscore and the next hyphen. Keep the source column formatted as text so its contents are preserved.

Solution 1Recommended

Extract the code with Power Query

Use Power Query to transform a large dataset and refresh the result when the source changes.

1
Open the data in Power Query

Select a cell in the source data and choose Data > From Table/Range. Confirm the table range and headers if prompted.

2
Split at the final underscore

Select the text column, then choose Home > Split Column > By Delimiter. Select underscore as the delimiter, choose the right-most delimiter, and select OK.

3
Split at the hyphen

Select the column containing the text after the final underscore. Choose Split Column > By Delimiter, select hyphen, and split at the left-most delimiter.

4
Keep and load the result

Keep the column before the hyphen, rename it Code, and remove unwanted split columns if needed. Choose Home > Close & Load to return the results to Excel.

Extract the code with Power Query
Plan for a large result: An Excel worksheet has a limit of 1,048,576 rows, including headers. Loading and refreshing a large query can also require substantial memory.
Extract codes in WPS Spreadsheet

Extract the code with WPS Spreadsheet formulas

Use a helper column to locate the final underscore, then extract the text up to the following hyphen in WPS Spreadsheet.

  1. 1. Open the workbook: Open the file in WPS Spreadsheet and identify the source text column. The example below assumes source text starts in A2 and columns B and C are empty.
  2. 2. Find the final underscore: In B2, enter =FIND("~",SUBSTITUTE(A2,"_","~",LEN(A2)-LEN(SUBSTITUTE(A2,"_","")))). Use this formula only if the source strings do not contain a tilde.
  3. 3. Extract the code: In C2, enter =MID(A2,B2+1,FIND("-",A2,B2+1)-B2-1). For Warkaa_SAM3994-2, this returns SAM3994.
  4. 4. Fill the formulas down: Select B2:C2 and use the fill handle to copy both formulas down the source rows. Check any errors for missing delimiters.
Extracts codes after the final underscoreWorks with Microsoft Excel XLSX and XLS filesUses familiar spreadsheet formulas and a familiar interface
microsoft office alternative - wps office

Frequently Asked Questions

How do I extract the code if the text has multiple underscores?

In Excel, enter =TEXTBEFORE(TEXTAFTER(A2,"_",-1),"-") in an empty cell. The -1 tells TEXTAFTER to find the final underscore.

What happens if an underscore or hyphen is missing?

The formulas can return an error. Check the source cell for a final underscore followed by a hyphen, or use IFERROR around the Excel formula to display a custom message.

Does the formula check for exactly three letters and four digits?

No. It extracts all text between the final underscore and the following hyphen, regardless of length or character type. A separate validation rule is needed to enforce the code pattern.

Can Power Query handle one million source rows?

Power Query can process large datasets, but performance depends on available memory and the transformations used. If loading to a worksheet, allow for Excel's 1,048,576-row limit, including the header.

How do I update the Power Query results after changing the source?

After changing the source table, select Data > Refresh All in Excel to rerun the query and update the loaded results.