Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


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").

↑ Back to the top


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.

↑ Back to the top


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

↑ Back to the top


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.

↑ Back to the top


Keywords: KB213476, kbprogramming, kbprb, kbdtacode

↑ Back to the top

Article Info
Article ID : 213476
Revision : 6
Created on : 1/24/2007
Published on : 1/24/2007
Exists online : False
Views : 257