XL: Unexpected Results with Mixed Text and Numbers in Lookup Table (213476)
The information in this article applies to:
- Microsoft Excel 2000
- Microsoft Excel 2002
- Microsoft Excel 97 for Windows
This article was previously published under Q213476 For a Microsoft Excel 7.0 and earlier version of this article, see 113261.
For a Microsoft Excel 98 version of this article, see 200855.
SYMPTOMS
In Microsoft Excel 97 and later, when you create a lookup table, the lookup functions may return the #N/A error value, or, if you are using the functions in a Microsoft Visual Basic for Applications procedure or macro, you may receive a "type mismatch" error, even though the values in the lookup table are sorted in ascending order and the value being looked up appears to fall within the range of the lookup table values.
This behavior occurs with VLOOKUP(), HLOOKUP(), LOOKUP(), and MATCH() with Match Type arguments of 1 or negative one -1.
CAUSE
This behavior can occur when text values and numeric values are mixed in a lookup table. When you look up a text value, Microsoft Excel looks only at the text values in your table and any numeric values will be overlooked. Similarly, when you look up a numeric value, Microsoft looks only at the numeric values, as in the following example:
- Start Excel, and then create the following spreadsheet:
A1: 3
A2: 1
A3: c
A4: 2
A5: d
A6: b
- Select cells A1:A6. On the Data menu, click Sort.
- In the Sort dialog box, click Ascending under Sort by, and then click OK.
- The spreadsheet now appears as:
A1: 1
A2: 2
A3: 3
A4: b
A5: c
A6: d
- Type the following formula in any empty cell:
The formula returns #N/A instead of the expected 3 (the largest value before the lookup value of "a").
RESOLUTION
To resolve this behavior, change all of the characters to text, use a cell reference, and enter the formula as an array, as in the following steps: NOTE: The steps in this example are a continuation of the example in the "Cause" section of this article.
- Type the following in cell B1:
- Type the following formula in cell B2 (or any empty cell)
=VLOOKUP(TEXT(B1,"@"),TEXT(A1:A6,"@"),1) - Press CTRL+SHIFT+ENTER to enter the formula as an array formula.
- The formula returns "3", the largest value before the lookup value of "a" referenced by cell B1.
REFERENCESFor more information about the Vlookup() worksheet function, click Microsoft Excel Help on the Help menu, type vlookup in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
For more information about the Hlookup() worksheet function, click Microsoft Excel Help on the Help menu, type hlookup in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
For more information about the Lookup() worksheet function, click Microsoft Excel Help on the Help menu, type lookup in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
For more information about the Match() worksheet function, click Microsoft Excel Help on the Help menu, type match in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
Modification Type: | Major | Last Reviewed: | 5/28/2003 |
---|
Keywords: | kbdtacode kbprb kbProgramming KB213476 |
---|
|