How to Combine VLOOKUP or XLOOKUP with TEXTBEFORE in Excel
Question details
The user needs to combine VLOOKUP or XLOOKUP with text extraction functions like TEXTBEFORE, but encounters mismatch errors between text and numeric data types.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Extracting specific characters from a string using TEXTBEFORE or TEXTAFTER and immediately using that result as the lookup value in an XLOOKUP or VLOOKUP formula.
- Observed behavior
- The lookup function fails to find a match and returns an error because TEXTBEFORE extracts values as text strings, which do not match the numeric format of the lookup array.
Check your source data to determine if the lookup array contains numbers or text. Identifying the data type difference is the key to resolving this formula error.
Convert the Lookup Range to Text within the Formula
Append an empty string to your lookup array to force Excel to treat the numeric lookup values as text, matching the output of TEXTBEFORE.
Since TEXTBEFORE always outputs a text string, looking up this text against a range of numbers will fail. By appending an ampersand and empty quotes to the lookup range, you instantly convert all numbers in that range to text within the formula's memory.
Click on the cell where you want the final lookup result to appear.
Type the beginning of your XLOOKUP formula using TEXTBEFORE as the lookup value, for example: =XLOOKUP(TEXTBEFORE(E2,"_"),
Enter your lookup range and append &"" to it. For example, $A$1:$A$22&"".
Add your return array and close the parentheses. The complete formula should look like: =XLOOKUP(TEXTBEFORE(E2,"_"),$A$1:$A$22&"",$B$1:$B$22).
Press Enter to calculate the result. The formula will now correctly match the text output to the modified text range.

Convert the Extracted TEXTBEFORE Value to a Number
Use a mathematical operation to convert the text result of TEXTBEFORE into a number before performing the lookup.
Use WPS Spreadsheet to Combine Advanced Lookup and Text Functions
WPS Spreadsheet provides powerful dynamic array capabilities, allowing you to easily combine XLOOKUP with text extraction functions like TEXTBEFORE. It processes array modifications instantly, making complex data retrieval smooth and error-free.
- 1. Open your workbook in WPS: Launch WPS Spreadsheet and open the document containing your dataset.
- 2. Select the destination cell: Click on the cell where you want to output the lookup result.
- 3. Enter the combined formula: Type your XLOOKUP and TEXTBEFORE formula, applying either the text conversion (&"") or number conversion (--) technique.
- 4. Calculate the result: Press Enter to instantly fetch and display the matched data.

Frequently Asked Questions
Why does XLOOKUP return an #N/A error when using TEXTBEFORE?
XLOOKUP requires an exact match of both value and data type by default. TEXTBEFORE extracts data as a text string, so if you are trying to look it up in a column formatted as numbers, XLOOKUP will not recognize it as a match. You must align the data types by converting the text to a number or the number range to text.
Can I use VLOOKUP instead of XLOOKUP to do this?
Yes, you can use VLOOKUP, but the best approach is to convert the TEXTBEFORE result to a number (using VALUE or --) rather than altering the lookup array. For example: =VLOOKUP(--TEXTBEFORE(E2,"_"), A1:C22, 2, FALSE).
What if the text string contains multiple delimiters?
The TEXTBEFORE function includes an optional 'instance_num' argument. You can add a third argument to specify which delimiter instance to use (e.g., TEXTBEFORE(E2, "_", 2) extracts all text before the second underscore).
Does TEXTAFTER cause the same data type issues?
Yes. TEXTAFTER also returns text values, so if you are extracting numeric characters and using them as a lookup value against a range of numbers, you will encounter the same mismatch error. The same conversion solutions apply.




