{"id":1106,"date":"2010-06-10T17:23:00","date_gmt":"2010-06-10T09:23:00","guid":{"rendered":"http:\/\/vm-officeblogs.cloudapp.net\/2010\/06\/10\/solutions-to-three-common-problems-when-using-vlookup\/"},"modified":"2022-07-26T04:17:42","modified_gmt":"2022-07-26T11:17:42","slug":"solutions-to-three-common-problems-when-using-vlookup","status":"publish","type":"post","link":"https:\/\/www.microsoft.com\/en-us\/microsoft-365\/blog\/2010\/06\/10\/solutions-to-three-common-problems-when-using-vlookup\/","title":{"rendered":"Solutions to Three Common Problems when Using VLOOKUP()"},"content":{"rendered":"
Today\u2019s author is Greg Truby, an Excel MVP, who addresses some common issues you may encounter when you use the VLOOKUP function.<\/em><\/p>\n
This article assumes a basic familiarity with the VLOOKUP() function, one of the easiest ways to lookup up a key value in one worksheet or block of data and return a related piece of information from a second worksheet or block of data. When using VLOOKUP() we frequently find ourselves facing three common problems:<\/p>\n
\n
\n
We need to look up based on more than one column<\/div>\n<\/li>\n
\n
We\u2019re getting an #N\/A though the key is valid<\/div>\n<\/li>\n
\n
We\u2019re confronted with the Zoolander problem<\/div>\n<\/li>\n<\/ul>\n
Problem #1: <\/strong>We need to look up something from a column, but we have to look up on two keys instead of one.\u00a0 Let\u2019s say we need to return the State information from the following table.<\/p>\n
\n\n
\n
\n
Produce <\/span><\/b><\/p>\n<\/td>\n
\n
Color <\/span><\/b><\/p>\n<\/td>\n
\n
State <\/span><\/b><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Apples <\/span><\/p>\n<\/td>\n
\n
Red <\/span><\/p>\n<\/td>\n
\n
Washington <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Cherries <\/span><\/p>\n<\/td>\n
\n
Red <\/span><\/p>\n<\/td>\n
\n
Michigan <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Bananas <\/span><\/p>\n<\/td>\n
\n
Yellow <\/span><\/p>\n<\/td>\n
\n
Hawaii <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Lemons <\/span><\/p>\n<\/td>\n
\n
Yellow <\/span><\/p>\n<\/td>\n
\n
Texas <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Grapes <\/span><\/p>\n<\/td>\n
\n
Green <\/span><\/p>\n<\/td>\n
\n
California <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Apples <\/span><\/p>\n<\/td>\n
\n
Green <\/span><\/p>\n<\/td>\n
\n
Pennsylvania <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Grapes <\/span><\/p>\n<\/td>\n
\n
Purple <\/span><\/p>\n<\/td>\n
\n
New York <\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
We cannot use just the Color column \u2013 would \u201cRed\u201d mean Washington?\u00a0 Or Michigan? We cannot use just the Produce column \u2013 would \u201cApples\u201d mean Washington? Or Pennsylvania?\u00a0 We need to look up on both Produce and Color.<\/p>\n
Solution to Problem #1:<\/strong> The simplest solution is to create a helper column that combines both keys at once. To help readability we can insert some type of delimiter between the two fields such as the pipe (|), a comma or a semicolon. When we combine two or more pieces of text (which are frequently called \u201ctext strings\u201d or more simply \u201cstrings\u201d) we call this combining process \u201cconcatenation\u201d. The concatenation operator in Excel is the ampersand (&) so our helper formula (using the pipe character) would be:<\/p>\n
After inserting the helper column, the formula and copying down, we end up with the following table. Note that our composite column still needs to be to the left of the column whose data we will be returning.<\/p>\n
\n\n
\n
\n
Produce <\/span><\/b><\/p>\n<\/td>\n
\n
Color <\/span><\/b><\/p>\n<\/td>\n
\n
Composite <\/span><\/b><\/p>\n<\/td>\n
\n
State <\/span><\/b><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Apples <\/span><\/p>\n<\/td>\n
\n
Red <\/span><\/p>\n<\/td>\n
\n
Apples|Red <\/span><\/b><\/p>\n<\/td>\n
\n
Washington <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Cherries <\/span><\/p>\n<\/td>\n
\n
Red <\/span><\/p>\n<\/td>\n
\n
Cherries|Red <\/span><\/b><\/p>\n<\/td>\n
\n
Michigan <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Bananas <\/span><\/p>\n<\/td>\n
\n
Yellow <\/span><\/p>\n<\/td>\n
\n
Bananas|Yellow <\/span><\/b><\/p>\n<\/td>\n
\n
Hawaii <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Lemons <\/span><\/p>\n<\/td>\n
\n
Yellow <\/span><\/p>\n<\/td>\n
\n
Lemons|Yellow <\/span><\/b><\/p>\n<\/td>\n
\n
Texas <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Grapes <\/span><\/p>\n<\/td>\n
\n
Green <\/span><\/p>\n<\/td>\n
\n
Grapes|Green <\/span><\/b><\/p>\n<\/td>\n
\n
California <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Apples <\/span><\/p>\n<\/td>\n
\n
Green <\/span><\/p>\n<\/td>\n
\n
Apples|Green <\/span><\/b><\/p>\n<\/td>\n
\n
Pennsylvania <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Grapes <\/span><\/p>\n<\/td>\n
\n
Purple <\/span><\/p>\n<\/td>\n
\n
Grapes|Purple <\/span><\/b><\/p>\n<\/td>\n
\n
New York <\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
We can create a similar composite column to use inside the VLOOKUP() or we can do the concatenation inside the VLOOKUP() formula.<\/p>\n
Problem #2:<\/strong> We know our data matches, but VLOOKUP() is returning #N\/A.<\/p>\n
Solution #2:<\/strong> The problem is almost always that the keys are a mixture of numeric values and text values within the cells and one of the key columns is formatted as GENERAL and the other is formatted as TEXT. Here the left table\u2019s key is formatted as General and the right\u2019s key is formatted as Text.<\/p>\n
The first solution that will normally occur to us is \u201cI\u2019ll just format the General column as Text\u201d (or vice-versa).\u00a0 So we highlight one of the columns and hit Ctrl+F1 (or Home | Format | Format Cells (2007, 2010) or Format | Cells\u2026 (2003 & below)) and change the format and \u2026. What!? It doesn\u2019t fix the problem. Changing a cell\u2019s format doesn\u2019t \u201ctake\u201d until you edit the cell.\u00a0 If we have more than just a few rows, we are not going to want to plink back and forth hitting F2 then ENTER a couple hundred times. We have two other options. One is to use Excel\u2019s built-in error correct if it has flagged this for us.\u00a0 In the screenshot below it has done so.\u00a0 We can then highlight all of the cells in that column and pick \u201cConvert to Number\u201d from the error correction popup menu.<\/p>\n
If we do not have the error correction option or if we simply prefer this method, we can use the Text to Columns<\/strong> tool instead. To use that we highlight the column whose format we wish to change and then from the menu (using Excel 2007 here) select Data | Text to Columns.<\/p>\n
We can just leave the DELIMITED option in place and click Next ><\/strong> and then make sure that whatever delimiter we have checked does not actually occur in your column. Normally just sticking with TAB will work fine since it very rarely occurs in a cell.\u00a0 Then we click Next ><\/strong> again and on Step 3 we pick the format we need General or Text and click Finish.<\/p>\n
Providing we correctly remembered what format we needed, we should be rewarded with the VLOOKUP() function working properly.<\/p>\n
Alternate Solution<\/strong> \u2013 if we\u2019re feeling adventurous, we can do the type conversion in-formula by coercing the data types. If the cells that go into the first VLookup argument are formatted as TEXT and the keys that are in the range that are in the second argument are formatted as GENERAL then something like:<\/p>\n
will coerce the first argument to GENERAL format whenever the data is numeric in nature.<\/p>\n
If it\u2019s the data that\u2019s in the key field of the 2nd argument\u2019s range that is TEXT format and our first argument\u2019s cell is GENERAL format then concatenating an empty string:<\/p>\n
will coerce the numbers in the first argument to all text. [Note: in these examples the second argument is a named range<\/strong>.]<\/p>\n
Next most likely solution<\/strong> – If the problem is not a mixture of text and numbers, then the next most common cause is extra spaces on one of the columns. One way we can quickly check this is by finding a cell that has returned #N\/A when it shouldn\u2019t and clicking on the key cell and hitting F2 and inspecting the cell\u2019s contents in the formula bar. If there are no leading or trailing spaces there, look at the corresponding key cell in the lookup range the same way. If we confirm that the problem is indeed being caused by leading or trailing spaces and if our data values have no spaces in the middle of them (our data reads \u201cExcelIsReallyFun\u00a0\u00a0\u00a0\u00a0 \u201d or \u201c\u00a0\u00a0\u00a0\u00a0 ExcelIsReallyRun\u201d; and not \u201c\u00a0 Excel Is Really Fun\u201d) then the simplest solution would be to just select the cells with too many spaces and hit Ctrl+H (Replace) and enter a space for the Find What argument and nothing at all for the Replace With argument.<\/p>\n
If there are spaces within the cells\u2019 values, one solution is to use the TRIM() function. If the extra spaces are in the first argument\u2019s cell, the solution is simple; we just insert TRIM in that first argument:<\/p>\n
If the extra spaces are in the lookup range then we can insert a helper column and use TRIM() in the helper column to eliminate leading and trailing spaces. We can then leave the helper column and change our VLOOKUP() to match on the helper column, or we can do a Copy and Paste Special\u2026 | Values over the top on the helper and then overwrite or delete the original column with the trimmed values. (The easiest way to access Paste Special regardless of version is to right-click once you have copied the cells.)<\/p>\n
If our data is always the same length after leading\/trailing spaces are removed then we could also use the Fixed Width option on the Text to Columns tool described above and split off any leading and trailing spaces and choose SKIP for the columns of extra spaces.<\/p>\n
Problem #3:<\/strong> A member of a leading online Excel forum once quipped that he hated the fact that VLOOKUP() is \u201cZoolander challenged\u201d. In the movie Zoolander<\/em>, Ben Stiller plays male model Derek Zoolander, who is not an \u201cambiturner\u201d. Derek can only turn right on the catwalk; he can\u2019t turn left. Likewise VLOOKUP() can only fetch back data from columns located to the right of the key, it cannot return from columns to the left. Let\u2019s say we need to return the type of flower given the one-letter key from the table below (we\u2019ll assume the table is located in A1:B6).<\/p>\n
\n\n
\n
\n
Flowers <\/span><\/b><\/p>\n<\/td>\n
\n
Key <\/span><\/b><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Roses <\/span><\/p>\n<\/td>\n
\n
R <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Carnations <\/span><\/p>\n<\/td>\n
\n
4 <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Lilies <\/span><\/p>\n<\/td>\n
\n
L <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Zinnias <\/span><\/p>\n<\/td>\n
\n
9 <\/span><\/p>\n<\/td>\n<\/tr>\n
\n
\n
Tulips <\/span><\/p>\n<\/td>\n
\n
T <\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
Solution #3:<\/strong> The way to return data from the left is to understand two additional functions, INDEX() and MATCH().\u00a0 The MATCH() function also looks something up for us. But instead of returning what\u2019s in the cell, it tells us the position in the list where it matches. The formula =MATCH(\u201cL\u201d,B1:B6,0) will return 4.<\/p>\n
The INDEX function returns the value of the contents of a range based on position. So the formula =INDEX(A1:A6, 4) will return \u201cLilies\u201d.<\/p>\n
So if we set that MATCH() inside the INDEX()<\/p>\n
We now have a formula that can look left and return \u201cLilies\u201d given the input \u201cL\u201d.<\/p>\n
Unfortunately, MATCH() suffers the same inability to convert between numbers and text as VLOOKUP(), so if we have a mix of General and Text, we need to use the same Text to Columns or in-formula coercions as described in Solution #2.<\/p>\n
Bonus<\/strong> \u2013 Once we become adept at using the team of INDEX() and MATCH() together, we can also employ them in an alternate solution to our composite key problem.\u00a0 Instead of inserting a helper column, we can have Excel do the concatenation inside the formula by entering the formula as an array formula.<\/p>\n
We type the formula in without the braces \u2013 the \u201c{\u201c and \u201c}\u201d \u2013 but instead of confirming the formula with a simple ENTER keystroke, we confirm it with CTRL+SHIFT+ENTER. To gain a better understanding of how the array formula actually works we can use the Evaluate Formula tool on the Formula tab in Excel 2007 & 2010, or from the Tools, Formula Auditing, Evaluate Formula\u2026<\/strong> path in Excel 2003. I leave it to the reader to do this exploration.<\/p>\n
In summary we now have solutions to three of our most frequent challenges in using the VLOOKUP() function:<\/p>\n
\n
We can now look up based on more than one column.<\/li>\n
We can troubleshoot cells where we\u2019re getting an #N\/A though the key is valid.<\/li>\n
We are now \u201cambiturners\u201d \u2013 we can fetch data from columns to the left of our key column.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"
This article assumes a basic familiarity with the VLOOKUP() function, one of the easiest ways to lookup up a key value in one worksheet or block of data and return a related piece of information from a second worksheet or block of data.<\/p>\n","protected":false},"author":64,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false,"_classifai_error":"","footnotes":""},"content-type":[4035],"product":[4047,4036],"audience":[4081,4101,4102],"tags":[],"coauthors":[4437],"class_list":["post-1106","post","type-post","status-publish","format-standard","hentry","content-type-tips-and-guides","product-excel","product-microsoft-365","audience-enterprise","audience-personal-and-family","audience-small-business"],"yoast_head":"\n
Solutions to Three Common Problems when Using VLOOKUP() | Microsoft 365 Blog<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\t\n\t\n\t\n