ADVANCED VLOOKUP USES AND FORMULAS
VLOOKUP is a powerful Excel formula that you can use to
capture data from a complex database and deliver it where you need it. When
used correctly, it can save you a ton of time and make you a more efficient and
proficient Excel user. The VLOOKUP syntax is:
=VLOOKUP(lookup_value,table_array,col_index_num,[range_lookup])
Argument
|
Meaning
|
Lookup_Value
|
The reference value, which can be a text, a numerical
string, or a cell whose value you want to reference.
|
Table_array
|
The overall data table. As such, the reference value
you are looking up should be in the first column of this table, column 1, so
Excel can move to its right and search for the return value.
|
Col_index_num
|
The column number where the return value is located.
This value starts at 1 and goes up relative to the number of columns in your
table.
|
[Range_lookup]
|
The fourth value is in brackets because
it is not a mandatory argument to make this function work. Brackets in Excel
syntax mean that the argument is optional. If you don’t complete this value,
Excel autoselects TRUE (or 1),
which means that you are not looking for an exact match to your reference
value, but an approximation. Using TRUE as the
value is not recommended for text returns.
|
To use VLOOKUP, you’ll need to supply (at a minimum) the
first three pieces of information. To input formulas in Excel, you can type
them directly into the cells or use the function wizard. It is difficult,
however, to write formulas in the wizard and check them as you go (pressing
F9). One tip with Excel is to write from the inside out. Since Excel computes
the innermost function(s) first, you should understand what each returns before
you wrap it into another function.
Example 1. Look up with 2 different criteria
Suppose
you have a list of orders and you want to find the Qty. based on 2 criteria -
"Customer Name" and "Product". A complicating factor is
that each customer ordered multiple products, as you see in the table below:
=VLOOKUP("Jeremy
Hill Sweets",$A$5:$C$14,3,FALSE)
or
=VLOOKUP(B1,$A$5:$C$14,3,FALSE)
Where
cell B1 contains your concatenated lookup value (lookup_value) and 3 is the number of
the column containing the data you want to find (col_index_num).
HOW TO GET ALL OCCURRENCES OF LOOKUP VALUE (DUPLICATES)
As
mentioned above, the Excel VLOOKUP function cannot get duplicate instances of
the lookup value. To do this, you would need a far more complex array formula
consisting of several Excel functions such as INDEX, SMALL and ROW.
For
instance, the below formula finds all instances of the value in cell F2 in the
lookup range B2:B16, and returns values from column C in the same rows:
{=IFERROR(INDEX($C$2:$C$16,
SMALL(IF($F$2=B2:B16, ROW(C2:C16)-1,""), ROW()-3)),"")}
Copy
the below formula to several adjacent cells, e.g. cells F4:F8 as demonstrated
in the screenshot below. The number of cells where you copy the formula should
be equal to or larger than the maximum number of possible duplicate entries.
Also, remember to press Ctrl + Shift + Enter to enter an array formula correctly.
If you are curious to know the underlying logic, let's drill
down into the formula a bit:
Part 1.
IF($F$2=B2:B16,ROW(C2:C16)-1,"")
$F$2=B2:B16
- compares the value in cell F2 with each value in the range
B2:B16. If a match is found, ROW(C2:C16)-1
returns the number of the corresponding row (-1 is used to
deduct the header row). If the compared values do not match, the IF function
returns an empty string.
The
result of the IF function is the following array {1, "", 3,
"", 5, "", "", "", "",
"", "",12, "", "", ""}
Part 2.
ROW()-3
In
this case, the ROW function acts as an incremental counter. Since the formula
is copied into cells F4:F9, we add -3 for the function to return 1 for cell F4
(row 4 minus 3), 2 for cell F5 (row 5 minus 3), etc.
Part 3.
SMALL(IF($F$2=$B$2:$B$16,ROW($C$2:$C$16)-1,""),ROW()-3))
The
SMALL function returns the k-th smallest value in a data set. In our case, the
position (from the smallest) to return is defined by the ROW function (Part 2).
So, for cell F4, the function SMALL({array},1) returns the 1st (smallest)
element of the array, i.e. 1. For cell F5, it returns the 2nd smallest element
of the array, which is 3, and so on.
Part 4.
INDEX($C$2:$C$16,
SMALL(IF($F$2=$B$2:$B$16, ROW($C$2:$C$16)-1,""), ROW()-3))
The
INDEX function simply returns the value of a specified cell in the array
C2:C16. For cell F4,
INDEX($C$2:$C$16,1)
returns "Apples"; for cell F5, INDEX($C$2:$C$16,3)
returns "Sweets", etc.
Part 5.
IFERROR()
Finally,
we wrap the formula in the IFERROR function, because you would hardly want to
see N/A error messages in your worksheet when the number of cells where you've
copied the formula is greater than the number of duplicate occurrences of the
lookup value.
0 Comments