Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Making VLOOKUP Case Sensitive.

Making VLOOKUP Case Sensitive

by Allen Wyatt
(last updated May 19, 2015)

3

Robin asked if there is a way to do a VLOOKUP that is case sensitive. Her lookup table/range has entries that are similar (AbC and aBC) with the only difference being the case of the letters. She can't change the values (make them all upper or lower case) since the unique values are vital.

The VLOOKUP function doesn't have a way to check for the case of information; it is case insensitive. There are several ways you can work around this shortcoming, however. One way is to use the CODE function to create an intermediate column that can be searched by VLOOKUP. Assuming that your original data is in column B, you could put the following formula in cell A1 and copy it down the column:

=CODE(LEFT(B1,1))&"."&CODE(MID(B1,2,1))&"."&CODE(RIGHT(B1,1))

This formula looks at the first three characters of whatever is in cell B1 and converts those characters to decimal character codes separated by periods. Thus, if A1 contained "ABC" then B1 would contain "65.66.67". Assuming that the value you want to locate is in cell C1, you could use the following as your VLOOKUP formula:

=VLOOKUP(CODE(LEFT(C1,1))&"."&CODE(MID(C1,2,1))&"."&
CODE(MID(C1,3,1)), A:B,2,)

Another approach is to use the EXACT function to determine the location of what you are looking for. This approach doesn't use VLOOKUP at all, instead relying on the INDEX function. The formula assumes that the cells you want to compare are in column A and what you want to return is the corresponding cell in column B.

=IF(MIN(IF(EXACT(C1,$A$1:$A$100),ROW($A$1:$A$100)))=0,NA(),
INDEX($B$1:$B$100,MIN(IF(EXACT(C1,$A$1:$A$100),ROW($A$1:$A$100)))))

This formula needs to be entered as an array formula (Shift+Ctrl+Enter). The first part of the formula (the first instance of EXACT) compares C1 (what you are looking for) to each value in the range A1:A100. Since this is an array formula, you end up with, in this case, 100 True/False values depending on whether there is an exact match or not. If there is a match, then the first ROW function returns the row of the match and the INDEX function is used to grab the value from column B in that row.

In some instances you might want to create your own user-defined function that will do the lookup for you. The following is an example of such a macro:

Function CaseVLook(compare_value, table_array As Range, _
  Optional col_index As Integer = 1)
    Dim c As Range
    Dim rngColumn1 As Range

    Application.Volatile

    Set rngColumn1 = table_array.Columns(1)
    CaseVLook = "Not Found"

    'Loop first column
    For Each c In rngColumn1.Cells
        If c.Value = compare_value Then
            CaseVLook = c.Offset(0, col_index - 1).Value
            Exit For
        End If
    Next c
End Function

To use the macro, simply call the function with the value you want to find (say cell C1), the range whose first column should be searched (such as A:B), and optionally the offset of the column within that range, as here:

=CaseVLook(C1,A:B,2)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12222) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Making VLOOKUP Case Sensitive.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Converting a Text Box to a Frame

These days, most people using Word know what text boxes are but have no idea about frames. Yet, for some purposes, frames are ...

Discover More

Using the WEEKNUM Function

Need to know which week of the year a particular date falls within? Excel provides the WEEKNUM function so you can easily ...

Discover More

Incrementing Months in Dates

Excel can easily store dates. If you want to increment a date by one month, there are a number of ways you can accomplish the ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Determining the Least Common Multiple

Need to figure out the least common multiple of a range of values? It is a snap when you use the LCM function, described in ...

Discover More

Nesting IF Worksheet Functions

The IF worksheet function is very handy to make conditional evaluations. You are not limited to a single IF comparison, ...

Discover More

Returning Blanks or Asterisks from a Lookup

Want to return more than a value when doing a lookup? Here are a couple of ways to do it by adding an IF clause to your ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 4?

2015-05-19 07:38:34

balthamossa2b

Completely agree with Bryan: Range.Find is way faster than manually looping, and it has IIRC a dedicated argument for case sensitivity.

Also INDEX-MATCH-MATCH is faster than an unsorted VLOOKUP for most array sizes, even more so if you use an additional line and column to store indexes.

Sorted VLOOKUP is still king though.


2013-10-18 07:15:08

Bryan

If you are using Option Compare Text the macro won't work any more. It's probably safer to replace the line

If c.Value = compare_value Then

with

If StrComp(c.Value, compare_value, vbBinaryCompare) = 0 Then

The StrComp function allows you to compare to strings without regard to the string comparison settings of the module.

While I'm at it, you can get rid of Application.Volatile, and the whole thing is very likely faster using the Range.Find method instead of looping through all the cells.


2012-10-20 14:47:36

Juan

The second formula is very complicated. Why do you use IF, MIN and another IF? What's the meaning of NA()?


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.