12
Excel’s lookup, reference, and information functions are quite different from the mathematical functions discussed in the last few chapters. Most math func- tions take your data as a starting point and use it to calculate some new result.
Lookup, reference, and information functions, on the other hand, don’t generate any new data. Instead, they let you search for and extract important bits of content from your worksheet and then reuse it in ways guaranteed to delight spreadsheet lovers worldwide.
All three types of functions play different but complementary roles. A lookup func- tion finds and then copies data from a particular cell in a worksheet. A reference function retrieves more general information about groups of cells (like how many columns’ worth of data is in your table). It’s common to use lookup and reference functions together. You can use a reference function to find out which cell has the data you want to use, and a lookup function to actually retrieve the cell’s contents.
Finally, information functions let you determine what kind of content resides in an individual cell (for instance, a number or text). Knowing that can help you construct extremely powerful conditional formulas, which behave differently depending on the type of data they encounter.
All these functions may not immediately seem useful, but they become indispensable in a variety of situations. Consider a few common examples:
• You want to let spreadsheet viewers look up items, and then see these items used in a series of calculations. Say you have a worksheet with a long list of customer records (each of which includes info like their mailing address and how much money they owe your company). You not only want to make the spreadsheet searchable, you also want to present your searchers with a series of calculations
Lookup, Reference, and Information Functions
ThE basiC
lookup about the items they’ve found, like how much money they owe your company after 30 days plus interest, 60 days plus interest and penalty, and so on. Lookup and reference functions let you turn your spreadsheets into extremely useful, interactive databases. It’s kind of like having an accountant-in-waiting for anyone who wants to ask questions about the information in your spreadsheet.
• You want to extract some data from a table and use it in another worksheet.
Maybe you have a list of mailing addresses, and you need to extract a specific address and place it at the top of a form letter. Lookup functions are perfect for that.
• Your worksheet has more than one table, and they’re all designed to work to- gether. Say you want to pluck out a product’s name and price from a massive table of product information, and then use it to create an invoice. This example represents one of the most powerful ways you can use lookup functions.
TIP Excel user groups and online forums are full of problems that you can solve only with lookup functions.
Try, for instance, the Excel forum on Microsoft’s Community site http://tinyurl.com/ms-excel-forum, or third-party discussion groups like www.msofficeforums.com/excel.
In this chapter, you’ll learn how to use the lookup and information functions, as well as how to overcome some of their limitations. Finally, you’ll consider the oddest lookup and reference function of them all: the HYPERLINK() function, which lets you create Web-style hyperlinks in a worksheet cell.
The Basic Lookup
VLOOKUP() and HLOOKUP() are the two most popular lookup functions. They per- form the same task—finding and copying data—but they look in different directions:
• VLOOKUP() is the vertical lookup function. You use it to find a specific row in a large table of data. VLOOKUP() works by scanning the values in a single column from top to bottom. Once the function finds the entry you’re looking for, it can retrieve other information from the same row. VLOOKUP(), exam- ined in closer detail in the next section, is the more commonly used of the two lookup functions.
• HLOOKUP() is the horizontal lookup function. You use it to find a specific column in a large table of data. HLOOKUP() works by scanning the values in a single row from left to right. Once the function finds the entry you’re looking for, it can retrieve other information from the same column.
ThE basiC lookup Both VLOOKUP() and HLOOKUP() require three parameters, and both have one
optional parameter:
VLOOKUP(search_for, table_range, column_number, [range_lookup]) HLOOKUP(search_for, table_range, row_number, [range_lookup])
The first parameter, search_for, is the cell content you’re trying to find. The second parameter, table_range, is the rectangular grid of cells that contains all the data you’re searching through. The column_number and row_number parameters are known as offsets. Once VLOOKUP() or HLOOKUP() finds the data you requested, the offset tells Excel how many cells to move over or down to find the related data you want.
Imagine a spreadsheet filled with customer records, each listed in its own row. You may initially use VLOOKUP() to find a customer’s last name, but what you’re really interested in is how much money she owes you. To find that crucial info, you’d use the offset argument to retrieve the value from a cell that’s a few columns away from the last name column.
Finally, the range_lookup parameter is a Boolean (true or false) value. If you specify TRUE, or leave out this parameter, Excel finds approximate matches. If you specify FALSE, Excel gives you either an exact match or, if it finds no match, the error value
#N/A. (You can also substitute 1 for TRUE or 0 for FALSE, if you find that’s easier.)
VLOOKUP(): Vertical Lookups
The simplest way to understand how lookup functions work is to study one of these functions in action. Looking at VLOOKUP() is a good place to start. Figure 12-1 shows a worksheet that uses VLOOKUP() to let spreadsheet viewers learn more about the products contained in a long list of exotic grocery store items. The idea here is to offer a kind of interactive information dashboard on top of the spreadsheet so that anyone can enter a product’s ID number, and then view specific information about the product (like the value of the inventory in stock).
True, you could simply construct your worksheet so that this information appears alongside the product data that’s already listed, but when you have multiple sce- narios that you want to let people play around with, your spreadsheet can get more crowded than a cross-town bus at 5:30 p.m. The functions in this chapter offer a better way to get customizable views for large amounts of information.
Here’s a blow-by-blow breakdown of what’s happening in Figure 12-1. The key cells are B2 and cells C4 through C11. B2 is where whoever’s reading the spreadsheet types in the product ID. C4 through C8 all contain different versions of the lookup function, which retrieve the product name, unit price, the number of units in stock, the number of units on order, and the reorder level, respectively. The actual calculations, which are pretty straightforward, take place in cells C9, C10, and C11.
ThE basiC lookup
FiGuRE 12-1 VLOOKUP() in action: The person using this spread- sheet simply types the appropriate product num- ber in cell B2. Then the lookup formulas in cells C4 through C8 automatically retrieve different kinds of price and inventory data (which starts in row 15).
The cells C9 through C11 don’t need any lookup functions—they simply use the values from the lookup cells to perform additional calculations.
Consider how Excel retrieves the product name, shown in cell C4. The formula is:
=VLOOKUP(B2, A15:F81, 2, FALSE) Here’s what you’re looking at:
• The first parameter, B2, is the product ID (which the person reading the spread- sheet types in). This is the value VLOOKUP() tries to match.
• The second parameter is the most important: It tells Excel what range of cells to search. This example has 66 product rows (from row 15 to 81) and six columns of data (from column A to column F). The first column in this range must be the column that contains the data you’re trying to locate (in this case, the product ID). The other columns contain the remaining information Excel is going to retrieve once it finds a match.
• The third parameter, 2, indicates the position of the data you want to retrieve.
Excel numbers each column in the range that you supply from left to right, starting at 1. Thus, 2 represents the Product Name column.
• Finally, FALSE indicates that you’re insisting on an exact match to ensure you have the correct product.
Now, if someone typed the product ID 21 into cell B2, for instance, each of the VLOOKUP() functions in cells C4 through C8 finds the requested row, and then
ThE basiC lookup gives you the data they’re designed to retrieve (product, price, the number of units
in stock, and the number of units on order, respectively).
TIP If you supply a range lookup value of TRUE, or if you omit this parameter altogether, Excel accepts a partial match when it performs the search. That is, it attempts to find an ID value that matches exactly, but if it can’t, it uses the closest ID that’s less than the lookup value in B2. As a result, it’s always a good idea to explicitly turn off range matching if you’re looking for specific items.
Once you retrieve the information you need, it’s fairly easy to use it in other ordinary calculations. For example, the formula in cell C9 (Figure 12-1) calculates the total value of all the products on order by multiplying the retrieved unit price by the retrieved number of units on order:
=C5*C6
NOTE Perhaps the most convenient feature of a worksheet that uses lookup functions is that the worksheet has to store only one copy of the data. So, in the previous example, if you modify the product list, the information you get from any linked lookup formulas updates automatically.
POWER USERS’ CLINIC
Advanced Text Lookup
The product lookup example shown in Figure 12-1 searches by product ID, which is a numeric value. You can also use lookup formulas to search text. You could modify the product lookup example to conduct searches using a product’s name. The price lookup formula would then look like this:
=VLOOKUP(B2, B15:F81, 2, FALSE)
This version of the price lookup formula has two changes (compared to the one on page 348). First, the range of searched data starts at B15 instead of A15. That’s because the column that Excel searches must always be the leftmost column. The second change is the offset. Because the first column is B, the VLOOKUP() function numbers that column as 1, and the adjacent Price column as 2 (instead of 3). Consequently, you need to modify your offset accordingly.
Text lookups are useful, but you may find them a bit cumber- some. As a rule of thumb, when you perform a text search, you
should always specify FALSE for the range lookup parameter.
Otherwise, the VLOOKUP() function will work erratically on unsorted lists, and it may fail to find the right match.
If you truly do need to perform partial text searches, you can use the asterisk (*) wildcard as a workaround. When you add an asterisk to the end of the search value, the VLOOKUP() function finds any cell that starts with this text. For example, if you type in Queso*, the VLOOKUP() function matches the Queso Cabrales product. You can even go one step further, and change your lookup formulas so they always accept partial matches. The trick is to add the asterisk onto the end of the cell value in your formula, as shown here:
=VLOOKUP(B2&"*", B15:F81, 2, FALSE) This formula uses the ampersand (&)—a special operator for joining text. For more information about manipulating text, see Chapter 11.
ThE basiC
lookup HLOOKUP(): Horizontal Lookups
Now that you’ve mastered vertical lookups, horizontal lookups will be a snap. The key distinction is that HLOOKUP() searches across the first row in the cell range you supply. You use the offset to retrieve data from other rows in the matching column.
Figure 12-2 shows an example of HLOOKUP() used with a grid of product information:
=HLOOKUP(C11,A1:E8,2)*C12
FiGuRE 12-2 Horizontal lookups are just as straightforward as vertical lookups; the direction is the only thing that changes. Here, Excel uses the function HLOOKUP() to scan the top row of product names.
This formula tells Excel to search the range of cells from A1:E8. Because this is a horizontal lookup, Excel starts out by inspecting the first row (cells A1 to E1), where it attempts to find a cell that matches the content in C11 (Queso Cabrales). When Excel finds a match, it uses the offset of 2 to step down to the second row and retrieve the cell value with the product price (21). Finally, the formula multiplies the retrieved price by the number of units (100), which are listed in cell C12.
IFNA(): Dealing with Failed Lookups
As you already learned, if you use a lookup function to search for something that doesn’t exist, the result is the #N/A error code, which means “not available.” For example, Figure 12-3 shows what happens if you enter the wrong product ID in the vertical lookup example you considered earlier.
ThE basiC lookup
FiGuRE 12-3 Excel can’t find a product with the ID 42093, so it shows the #N/A error code instead.
This is Excel’s standard behavior, and there’s nothing wrong with it. After all, the
#N/A error code points out that there’s a problem with your data. However, it might confuse less experienced Excel users. If you’re developing a workbook that other people will use, it’s worth taking some extra time to replace the #N/A code with a more meaningful message.
The easiest way to do that is to use Excel’s IFNA() function. It checks an expression to see if it causes a #N/A error code. If your lookup succeeds, the IFNA() function simply shows the lookup result. But if the lookup fails and returns the #N/A code, IFNA() swaps in the alternate value you specify.
For example, in Figure 12-3 this function performs the product name lookup:
=VLOOKUP(B2, A15:F81, 2, FALSE)
To replace the #N/A value with your own message, you wrap the VLOOKUP() func- tion in an IFNA() function, like so:
=IFNA(VLOOKUP(B2, A15:F81, 2, FALSE), "[ PRODUCT NOT FOUND ]")
Now, when the lookup function can’t find the text you want, [PRODUCT NOT FOUND]
appears in the cell, as shown in Figure 12-4.
NOTE IFNA() is new to Excel 2013. If you need backward compatibility with older versions of Excel, and you want to use the trick described here, you’ll need to rely on a more verbose and awkward solution: combining the IF() function (described on page 239) with the ISNA() function. Essentially, you use ISNA() to test for an #N/A value (it returns TRUE or FALSE), and then you use the IF() function to swap in the right value, depending on the result.
ThE basiC lookup
FiGuRE 12-4 This lookup function failed to find matching text, but now the worksheet explains what happened in cell C4. It also hides the
#N/A error messages that would ordinarily appear in the cells underneath.
You can work similar magic with the lookups for the other product information, includ- ing the price and stock numbers. Here, the IFNA() function simply substitutes a blank value (really an empty text string; see the formula below) if the price lookup fails:
=IFNA(VLOOKUP(B2, A15:F81, 3, FALSE), "")
There’s still one minor niggle in this example. When the lookup fails, any other func- tions that use the lookup data will fail, too, and report an #N/A error. For example, consider the reorder calculation in cell C8, which uses this formula:
=C5*C6
If the lookup fails, the IFNA() functions in cells C5 and C6 replace the lookups with empty text strings. This causes the reorder calculation to fail, because there’s no way to multiply two pieces of text together. So the result of the formula is the
#VALUE! error code.
NOTE If you create a formula that uses a true blank cell—one that has never been touched—Excel treats that cell as though it contains the number 0. But once you put something in a cell, there’s no way to return it to this blank, virgin state. The best you can do is to put in an empty string, and that will break any formulas that attempt to use the cell in a calculation.
There are two solutions. One is change the IFNA() functions that get the price and stock information. Instead of replacing #N/A values with empty text strings, they should swap in the number 0. That way, the equation won’t fail, because =0*0 is
aDvanCED lookups a perfectly reasonable calculation. The only problem is that all these zeros will be
visible in your worksheet. To hide them, you can style the cells with the Accounting number format (page 134) or a custom number format that tells it not to show zero values (page 143).
The other option is to use the IFERROR() function to tell Excel to show a blank string (nothing) if the formula fails:
=IFERROR(C5*C6, "")
The IFERROR() function works the same as the IFNA() function, except it checks the expression for any type of error, not just #N/A values.
Advanced Lookups
VLOOKUP() and HLOOKUP() work well for finding related information in different tables. They also impose a few restrictions, however, including:
• The lookup column has to be the first column or row in the range. That means you can’t use VLOOKUP() to retrieve columns to the left of the lookup column, and you can’t use HLOOKUP() to retrieve rows above the lookup row.
• You must choose between horizontal and vertical lookups. You can’t perform a lookup that finds cells based on a lookup column and a lookup row.
In this section, you’ll learn how to skirt these restrictions with the help of other functions.
TIP You simply can’t get around certain lookup rules. The lookup functions aren’t much use if you have potentially multiple matches. That means you can’t use a lookup function to retrieve your top-10 selling products, for example. If you want to use this sort of logic, then you should probably opt for Excel’s list feature (Chapter 14), which provides filtering capabilities.
MATCH(): Finding the Position of Items in a Range
The MATCH() function lets you find the position of an item in a range. On its own, MATCH() doesn’t accomplish a whole lot, but used in conjunction with some of the functions described later in this section, you’ll find it really handy. Here are some MATCH() fundamentals.
To use MATCH(), you simply specify the search value (either a number or text) and the range you want to search:
MATCH(search_for, range, [match_type])
The range you use must be one-dimensional. That means you can search through the column of cells A1:A10 or the row of cells A1:E1, but you can’t search the grid of cells A1:E10.