27
© 2017 Excel University ALL RIGHTS RESERVED VLOOKUP Hacks 5 Ways to Get More Use from VLOOKUP

VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

© 2017 Excel University ALL RIGHTS RESERVED

VLOOKUP Hacks

5 Ways to Get More Use from VLOOKUP

Page 2: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

VLOOKUPHack#1:SortOrder

IssueVLOOKUP Hack #1 helps address the sort issue. Sort issue? Yes, and the sort issue has confuzzled many an Excel user over the years.

Let’sDigIn!Let’s say we wanted to use VLOOKUP to retrieve an account name based on the account number from a chart of accounts.

Assuming the lookup value is B7, the lookup range is Table1, and the account

name is in the second column, we could use something like this in C7: =VLOOKUP(B7,Table1,2)\

When the table is sorted in ascending order by the lookup column, the AcctNum column, you get the expected result.

For example, if the AcctNum is 1002, the VLOOKUP function above returns the expected account name, Savings, as shown in C7 below.

Page 3: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

But, if the table get sorted by AcctName instead, we get an unexpected result, as shown below.

Wait, what? 1002 is not Checking account, yet, that is what VLOOKUP returns … is it broken? Is the formula wrong? What’s up? Do I have to sort by the lookup column? What is going on here?

Page 4: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

These questions all bring us to our first hack! So, the VLOOKUP formula above is written like this: =VLOOKUP(B7,Table1,2)

You will notice that 3 arguments are defined, B7, Table1, and 2.

But, here is the hack: there is an optional 4th argument!

When the 4th argument is omitted, as in the formula above, it takes on a default value.

So, we need to unpack this mysterious 4th argument to understand what is happening.

The 4th argument controls the behavior of VLOOKUP in a couple of key ways, including, as you may suspect, the sorting requirement. Officially, the 4th argument is called the range_lookup argument.

Practically, it tells Excel whether you are looking for an exact matching value, or, a value between a range of values. It is a Boolean argument, so it expects a TRUE or FALSE value. (Alternatively, you can use 0 instead of FALSE and any non-zero number instead of TRUE.) TRUE means you are looking for a value between a range of values, and FALSE means you are looking for an exact matching value.

I’ll expand this idea more later on, but for now, I want to stay focused on the sort issue.

Page 5: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

When the 4th argument is TRUE, the data must be sorted in ascending order to return an accurate result.

However, when the 4th argument is FALSE, the data need not be sorted…any order is fine.

So, when the 4th argument is TRUE, you may get an unexpected result if the data is not sorted in ascending order by the first column in the range. And, that is exactly what we saw above. We got an unexpected result.

Instead of 1002 returning Savings, it returned Checking account. But, if we inspect the formula, we can see that we did not specify TRUE or FALSE for the 4th argument.

We simply omitted it.

Here is the key: when omitted, the 4th argument is TRUE.

That means that when we write a VLOOKUP, and don’t specify the 4th argument, it defaults to TRUE. When the data is not sorted in ascending order by the first column, you may get unexpected results.

That means, sort order matters!

So, we can resolve the issue by using FALSE (or 0) as the 4th argument, as shown below. =VLOOKUP(B7,Table1,2,FALSE)

With that update to our formula, it returns Savings, as expected:

Page 6: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

So, remember this: when the 4th argument is TRUE or omitted, the lookup range must be sorted in ascending order by the first (lookup) column to return an accurate result. But, when the 4th argument is FALSE (or 0), the data does not need to be sorted.

VLOOKUPHack#2:RangeLookups

In the first VLOOKUP hack, we talked about how the 4th argument impacts the sort order. But, there is more to uncover about this 4th argument.

So far, we understand that the 4th argument tells Excel whether we are

looking for a value between a range of values or an exact matching value.

Its official name is range_lookup, and now it is time to dig into what it actually means.

When the 4th argument is TRUE, or omitted, it tells Excel to perform a range lookup.

What exactly is a range lookup?

It means you are looking for a value between a range of values. The fastest way to explain this is with an example and a picture.

Page 7: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

Let’s suppose the sales manager created a sales incentive program for the month. He would like you to pay a bonus amount to the sales reps based on their sales for the month.

He then provides you with the following table:

If a salesperson had sales of 1,200 for the month, you would easily find the bonus amount of 50. If a salesperson had sales of 12,345, you would be able to determine the bonus amount is 500.

When you are doing this manually, you aren’t looking for an exact matching sales amount. You are looking for a sales amount that falls between a start and end point.

That is EXACTLY what the 4th argument means! That is a “range lookup.”

So, when the 4th argument is TRUE, you are telling Excel to perform a range lookup. When the 4th argument is FALSE, you are telling Excel to find an exact matching value. Note: You may see 0 used instead of FALSE in the 4th argument. Excel evaluates 0 as FALSE, and any non-zero number as TRUE.

Page 8: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

Now that we have the overall concept down, let’s dig into the Excel details.

When we humans perform a range lookup, we love seeing both the start and end points. For example, in the sales bonus illustration above, there are From and To columns. Being able to see both sides of the range makes us feel warm and fuzzy. Content. Comfortable.

But, here is the hack: VLOOKUP only needs the From column!

The implications of this are important. So, let’s unpack them. First, here is an updated table that would work perfectly with VLOOKUP:

Here is how I like to think about VLOOKUP. I like to think about it operating in two stages. In stage one, it looks in the first column ONLY. It starts at the top, and goes down one row at a time looking for its matching value. Once it finds it match, then it enters stage two, where it shoots to the right to retrieve the related value.

So, when the 4th argument is TRUE (or omitted), it will look down the Sales column until it finds its row. Any sales amount that is >= 0 and < 1,000 will return a

Page 9: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

bonus amount of 0. And sales >= 1,000 and < 2,500 will return 50. And so on.

Now that we see how this works, it is easy to understand why the data must be sorted in ascending order when the 4th argument is TRUE.

In order for VLOOKUP to return an accurate result when doing a range lookup, the table must be sorted in ascending order by the lookup column. Hopefully, this helps clarify the sort order issue, which we discussed at length in the first VLOOKUP hack.

Let’s explore this capability with a few examples.

Example1:BonusIf we wanted Excel to determine the bonus amount based on sales, we would write the

following formula in to cell C7 to retrieve the bonus amount from Table1: =VLOOKUP(B7,Table1,2,TRUE)

When the sales amount is 12,345, VLOOKUP returns the expected bonus amount of 500, as shown below.

That is the basic operation of range lookups, but, we can apply this in many different ways. For example, we can do a lookup on date values.

Page 10: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

Example2:FiscalPeriods

Fiscal periods…you mean dates? Yes … VLOOKUP can even work with dates! For example, let’s say we need to create fiscal periods based on transaction dates. We could set up a fiscal period table (Table3), like this one:

Then, it would be easy to have VLOOKUP retrieve the corresponding quarter label for a set of transactions. For example, we could use VLOOKUP to populate column D shown below.

The formula written into D15 and then filled down, is: =VLOOKUP(B15,Table3,2,TRUE)

Example3:SingleColumn

We can even do a range lookup on a single-column lookup table. This technique provides an easy way to return the beginning point of a range.

For example, if we need to find the pay period begin date, we could

Page 11: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

create a table of pay period begin dates, like this:

Then, we can use VLOOKUP to return the value from the 1st column of the table (Table4), like this: =VLOOKUP(B18,Table4,1,TRUE)

We could write the formula into D18 and fill it down, as shown below:

So, that is what it means to perform a range lookup. In the next post, we’ll talk more about the implications of the 4th argument, and how we can use it to help us perform list comparisons, aka, reconciliations!

Sample Excel File:

Page 12: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

VLOOKUPHack#3:ExactMatch

For the 3rd VLOOKUP hack we’ll dig even deeper into the 4th argument. We’ll see how we can leverage it to help with our reconciliations and other list comparisons.

Let’s pretend for a moment that we need to compare two lists. We’d like to find out which items on one list appear on the other list. This type of thing is often referred to as a list comparison. Accountants often refer to this task as a reconciliation.

So, let’s use VLOOKUP to complete one of the most common reconciliations of all, a bank rec.

DetailedStepsIn a bank rec, one list is the check register, and the other list is the bank statement. Here is a way-simplified check register:

And here is the activity per the bank we downloaded from the bank website:

Page 13: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

Now, we have a simple question. Which checks in the check register have cleared the bank? That is, which checks on the check register are also found in the bank activity table.

When a check is found in the bank activity table, we want to return the amount. This sounds like a job for VLOOKUP.

So, we write the following formula =VLOOKUP([Ck Num],Table1,2)

We get these results:

Specifically, we get an amount for every check, even those that haven’t cleared the bank!

Specifically, checks 1003 and 1005 are not in the bank table, but, the formula retrieves a value for them. So, what are we supposed to do? Do the bank rec manually? No! This brings us to our next hack.

HackIn the formula above, the 4th argument is omitted, so, it is performing a range lookup. We discussed this in detail in the previous post. When VLOOKUP performs a range lookup, it is not looking for an exact matching value.

Instead, it is looking for a value that falls between a range of values. For example, 1003 returns 110 from the bank activity table because 1003 is >=1002

Page 14: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

and <1004. Upon reflection, this is exactly what we would expect after understanding range lookups from the previous post. Now, here’s the hack that will help.

Hack: when the 4th argument is FALSE, VLOOKUP returns #N/A when no matching value is found!

So, let’s modify the formula above by using FALSE (or 0) as the 4th argument: =VLOOKUP([Ck Num],Table1,2,FALSE)

Yes … it worked! The update is shown below.

Now, we can easily see which checks have cleared the bank (those where the amount per bank is returned), and which are still outstanding (#N/A).

So, we are good, right? Well, not so fast. You see, the #N/A error will trickle down to mess up any formulas that include the range.

For example, perhaps we want to compute a total, as shown below.

Page 15: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

So, is there a way to clean up the #N/A errors, so that the reconciliation will look cleaner and so that the total formula won’t be broken? Yes…we can just get a little help from the IFERROR function.

In summary, IFERROR will return its first argument, unless it is an error. If so, it will return the second argument instead.

So, we can replace the #N/A with something else, for example, we could replace it with 0 like this: =IFERROR(VLOOKUP([Ck Num],Table1,2,FALSE),0)

Or, we could replace it with a text string by enclosing the text string in quotes, like this: =IFERROR(VLOOKUP([Ck Num],Table1,2,FALSE),"Outstanding")

Here’s what that will look like:

Now it is very clear which checks are still outstanding, and the total works!

VLOOKUPHack#4:ColumnLabels

Now, we are going to explore a hack for the 3rd argument. Here, we’ll hack the 3rd argument so that it references column labels instead of the column position. Check it.

Page 16: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

ObjectiveHere’s the deal. The 3rd argument of the VLOOKUP function is officially known as col_index_num. This represents the position of the value you want returned.

For example, if you want to return the amount from the 2nd position, or column, within the lookup range, you would enter 2 for the argument. Consider the screenshot below.

To return the amount from the 2nd column in Table1, we could use the following formula written into C5: =VLOOKUP(B5, Table1, 2, 0)

But, what if we wanted to communicate with Excel using the column label (Amount) instead of an integer value (2). For example, we wanted to do something like this: =VLOOKUP(B5, Table1, "Amount", 0)

But, when we enter such a formula we get an error. So, apparently Excel does not allow us to reference the column using the column label. Or…does it?

Page 17: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

Hack

Essentially, we would like Excel to translate the column label (Amount) into the corresponding integer (2). Here’s how.

Hack: use MATCH instead of an integer for the 3rd argument.

The MATCH function returns the relative position of a list item. So, we will ask the MATCH function to find the label (Amount) within the table header row, and return the position number. Then, VLOOKUP will use that position number.

For example, the following formula would return 2 since Amount is the second column label within the table:

=MATCH("Amount", Table1[#Headers], 0)

But, since “Amount” is already entered into cell C4, we can simple reference C4 instead, as follows:

=MATCH(C4, Table1[#Headers], 0)

This MATCH function would return 2 since the Amount label is in the 2nd table column. So, replacing the 2 in our original formula with the MATCH function would look like this: =VLOOKUP(B5, Table1, MATCH(C4,Table1[#Headers],0), 0)

This technique allows us to reference the column labels instead of the position number. But, Jeff, hang on. That seems WAY more complicated than just using an integer. Why would we want to

Page 18: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

go through such trouble? Well, let’s check out a few fun applications of this technique.

Applications

Application 1: Column order changes

If the column order changes, a traditional VLOOKUP function (written with an integer 3rd argument) will break. Why? Because Excel doesn’t update the integer value accordingly. Using MATCH instead prevents this error, making your workbook more reliable and efficient.

For example, First name is originally in the 2nd column. But, then we move it to the 3rd column, as illustrated below.

Using the MATCH function as the 3rd argument allows the VLOOKUP to continue to retrieve the desired

Page 19: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

First name, even as the column order changes.

Note: the lookup column must continue to be the first column within the lookup range.

Application 2: Insert new columns

If a new worksheet column is inserted between the lookup column and the return column, a traditional VLOOKUP function will break because Excel won’t update the integer value accordingly.

But, using MATCH instead of an integer enables the VLOOKUP function to automatically adapt. This reduces the likelihood of an error and improves efficiency since you won’t need to rewrite the formula after inserting a new column.

For example, we return the State column, as shown below.

Later, we insert a new column Address2. Our hacked VLOOKUP function continues to return the State code without modification, as shown below.

Page 20: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

Application 3: Two-dimensional lookups

When we think about traditional VLOOKUP functions, we think of them as searching for a matching value down, vertically.

But, with MATCH, we can perform two-dimensional lookups, where VLOOKUP searches down through the rows and MATCH searches across the columns.

This opens up some interesting possibilities. For example, let’s say we have some inventory items that we sell for

different amounts, depending on the discount level of the customer.

We could create a master table of items, and use one column for each pricing level or tier. Then, we could define the tier in an input cell (C7), and have Excel search down the list for the matching item, and then right to find the price based on the specified tier.

Page 21: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

And these are just a few ways to apply the MATCH hack to VLOOKUP! If you have any other VLOOKUP hacks, please share by posting a comment below…thanks!

VLOOKUPHack#5:DifferentTables

We are right in the middle of a blog series called VLOOKUP Hacks. We started by exploring the 4th VLOOKUP argument. Then we hacked the 3rd argument.

Now, as you may have imagined, it is time to hack the 2rd argument. In this post, we'll allow the user to retrieve values from different lookup tables.

ObjectiveBefore we get too far, let's be clear about the goal. The goal is to write a VLOOKUP function that will retrieve a value from a table that the user specifies.

That is, there are a bunch of different lookup tables, and we want VLOOKUP to retrieve a value from whichever table the user identifies. I've created a short video demonstration as well as a written narrative for reference.

We would like to be able to enter a taxable income amount into a cell, and have Excel compute the correct tax amount. To compute the tax amount, we need to know a few things.

Page 22: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

For starters, we need to know the taxable income. So, we create a little input cell (D7) to store the taxable income, as shown below.

Then, we need to know the tax rates. We get the tax rates from the IRS and enter them into a table (named Single), as shown below.

We'll need to retrieve several values from the table in order to compute the correct tax amount. Let's start by retrieving the marginal tax rate. The tax rate is in the 2nd column within the table (named Single), so, we write the following formula.

=VLOOKUP(D7,Single,2,TRUE)

When we inspect the formula result in D8, we confirm it returns 25% when taxable income is 50,000, so we are off to a great start, as shown below.

Page 23: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

The VLOOKUP function retrieves the rate from the Single table. But, we know that the IRS creates separate tax tables for each filing status. So, we create one table for each filing status, as shown below.

These tables are named Single, MFJ, HH, and MFS. Now, we just need to allow the user to identify the correct filing status table.

So, we quickly update our workbook, and add an input cell D6 to allow the user to

Page 24: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

enter the desired table name, as shown below.

Now, we just need to update our formula to refer to the tax table input cell D6 instead of the table name. So, we just update the 2nd argument accordingly:

=VLOOKUP(D7,D6,2,TRUE)

But, drats! We get an error, as shown below.

So, apparently, VLOOKUP doesn't allow us to identify the table name. Or...does it?

That brings us to the hack!

HackWe can specify the lookup table, but, we need an assist from another function. So, here is the hack.

Hack: Use INDIRECT as the 2nd argument

The INDIRECT function converts a text value, such as "Single" in D6, into a valid Excel reference. That is, it will convert the text string "Single" into the corresponding table name Single.

We update our formula by wrapping the INDIRECT function around the input cell D6 reference, as shown below:

Page 25: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

=VLOOKUP(D7,INDIRECT(D6),2,TRUE)

And, yes...it works!

Now that we've got that part working, we can finish out the tax calculation as follows.

The next step is to compute the marginal income that will be taxed at the marginal rate. In theory, this is done by taking the taxable income amount (eg, 50,000) and then subtracting the beginning amount of the corresponding tax bracket (eg, 37,950).

Fortunately, we discussed how to retrieve the beginning point of a range in a previous post by using 1 as the 3rd

argument. So, the formula would be something like this: =D7-VLOOKUP(D7,INDIRECT(D6),1,TRUE)

The results are shown in D9 below.

We can then compute the marginal tax by multiplying the marginal income by the marginal rate.

And finally, we need to retrieve the amount from the Plus column (so that we can add it to the marginal tax amount) with the following formula:

Page 26: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

=VLOOKUP(D7,INDIRECT(D6),3,TRUE)

We add the retrieved amount to the marginal tax, and we have the Total Tax amount, as shown below.

With the formulas complete, we are now able to easily enter the taxable income and tax table into the input cells, and Excel computes the corresponding tax. Nice!

Of course, we could combine these into a single formula, and, provide an in-cell drop down with data validation for the user to select the table. The sample file includes these enhancements in case you'd like to check them out.

ConclusionHopefully, these items will help you get more mileage out of VLOOKUP. It is an amazing function!

Also, I have a few more useful tricks, so, please stay tuned for additional VLOOKUP hacks!

Page 27: VLOOKUP Hacks - Excel University€¦ · VLOOKUP Hacks © 2017 Excel University But, if the table get sorted by AcctName instead, we get an unexpected result, as

VLOOKUP Hacks © 2017 Excel University

Sample Files: Hack 1: https://www.excel-university.com/vlookup-hack-1-sort-order/

Hack 2: https://www.excel-university.com/vlookup-hack-2-range-lookups/

Hack 3: https://www.excel-university.com/vlookup-hack-3-exact-match

Hack 4: https://www.excel-university.com/vlookup-hack-4-column-labels/

Hack 5: https://www.excel-university.com/vlookup-hack-5-different-tables/