Extract a Three-Letter, Four-Digit Code from Excel Text
Question details
Extract a code containing three letters and four digits from text strings.

- 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.
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.
Extract the code with Power Query
Use Power Query to transform a large dataset and refresh the result when the source changes.
Select a cell in the source data and choose Data > From Table/Range. Confirm the table range and headers if prompted.
Select the text column, then choose Home > Split Column > By Delimiter. Select underscore as the delimiter, choose the right-most delimiter, and select OK.
Select the column containing the text after the final underscore. Choose Split Column > By Delimiter, select hyphen, and split at the left-most delimiter.
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 an Excel formula
Use this formula when your Excel version supports TEXTAFTER and TEXTBEFORE.
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. 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. 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. Extract the code: In C2, enter =MID(A2,B2+1,FIND("-",A2,B2+1)-B2-1). For Warkaa_SAM3994-2, this returns SAM3994.
- 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.

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.




