XL: Unexpected Results with Mixed Text and Numbers in Lookup Table

Article translations Article translations
Article ID: 213476 - View products that this article applies to.
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.
Expand all | Collapse all

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:
  1. Start Excel, and then create the following spreadsheet:
       A1: 3
       A2: 1
       A3: c
       A4: 2
       A5: d
       A6: b
    					
  2. Select cells A1:A6. On the Data menu, click Sort.
  3. In the Sort dialog box, click Ascending under Sort by, and then click OK.
  4. The spreadsheet now appears as:
       A1: 1
       A2: 2
       A3: 3
       A4: b
       A5: c
       A6: d
    					
  5. Type the following formula in any empty cell:
    =VLOOKUP("a",A1:A6,1)
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.
  1. Type the following in cell B1:
    a
  2. Type the following formula in cell B2 (or any empty cell)
    =VLOOKUP(TEXT(B1,"@"),TEXT(A1:A6,"@"),1)
  3. Press CTRL+SHIFT+ENTER to enter the formula as an array formula.
  4. The formula returns "3", the largest value before the lookup value of "a" referenced by cell B1.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS A Microsoft Visual Basic macro that uses a lookup function returns the following run-time error message
Type mismatch
when you search a range that includes both text and numeric values. To illustrate this behavior, use the sample Visual Basic macro in the following example:
  1. Repeat steps 1 through 5 of the example in the "Cause" section.
  2. Select cells A1:A6.
  3. On the Insert menu, point to Name, and then click Define.
  4. In the Names in workbook box, type hi and then click OK.
  5. Press ALT+F11 to start the Visual Basic editor.
  6. On the Insert menu, click Module.
  7. In the module sheet, type the following code:
    Sub Test()
       MsgBox Application. _
       VLookup("b1", Workbooks("book1.xls"). _
       Worksheets("sheet1").Range("hi"), 1, False)
    End Sub
    					
  8. Press ALT+F11 to return to Excel.
  9. On the Tools menu, point to Macro, and then click Macros.
  10. In the Macro name box, click Test, and then click Run.
  11. The macro returns the following run time error message:
    Type mismatch

REFERENCES

For 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.

Properties

Article ID: 213476 - Last Review: January 24, 2007 - Revision: 2.4
APPLIES TO
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 97 Standard Edition
Keywords: 
kbdtacode kbprb kbprogramming KB213476

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com