- •About the Author
- •About the Technical Editor
- •Credits
- •Is This Book for You?
- •Software Versions
- •Conventions This Book Uses
- •What the Icons Mean
- •How This Book Is Organized
- •How to Use This Book
- •What’s on the Companion CD
- •What Is Excel Good For?
- •What’s New in Excel 2010?
- •Moving around a Worksheet
- •Introducing the Ribbon
- •Using Shortcut Menus
- •Customizing Your Quick Access Toolbar
- •Working with Dialog Boxes
- •Using the Task Pane
- •Creating Your First Excel Worksheet
- •Entering Text and Values into Your Worksheets
- •Entering Dates and Times into Your Worksheets
- •Modifying Cell Contents
- •Applying Number Formatting
- •Controlling the Worksheet View
- •Working with Rows and Columns
- •Understanding Cells and Ranges
- •Copying or Moving Ranges
- •Using Names to Work with Ranges
- •Adding Comments to Cells
- •What Is a Table?
- •Creating a Table
- •Changing the Look of a Table
- •Working with Tables
- •Getting to Know the Formatting Tools
- •Changing Text Alignment
- •Using Colors and Shading
- •Adding Borders and Lines
- •Adding a Background Image to a Worksheet
- •Using Named Styles for Easier Formatting
- •Understanding Document Themes
- •Creating a New Workbook
- •Opening an Existing Workbook
- •Saving a Workbook
- •Using AutoRecover
- •Specifying a Password
- •Organizing Your Files
- •Other Workbook Info Options
- •Closing Workbooks
- •Safeguarding Your Work
- •Excel File Compatibility
- •Exploring Excel Templates
- •Understanding Custom Excel Templates
- •Printing with One Click
- •Changing Your Page View
- •Adjusting Common Page Setup Settings
- •Adding a Header or Footer to Your Reports
- •Copying Page Setup Settings across Sheets
- •Preventing Certain Cells from Being Printed
- •Preventing Objects from Being Printed
- •Creating Custom Views of Your Worksheet
- •Understanding Formula Basics
- •Entering Formulas into Your Worksheets
- •Editing Formulas
- •Using Cell References in Formulas
- •Using Formulas in Tables
- •Correcting Common Formula Errors
- •Using Advanced Naming Techniques
- •Tips for Working with Formulas
- •A Few Words about Text
- •Text Functions
- •Advanced Text Formulas
- •Date-Related Worksheet Functions
- •Time-Related Functions
- •Basic Counting Formulas
- •Advanced Counting Formulas
- •Summing Formulas
- •Conditional Sums Using a Single Criterion
- •Conditional Sums Using Multiple Criteria
- •Introducing Lookup Formulas
- •Functions Relevant to Lookups
- •Basic Lookup Formulas
- •Specialized Lookup Formulas
- •The Time Value of Money
- •Loan Calculations
- •Investment Calculations
- •Depreciation Calculations
- •Understanding Array Formulas
- •Understanding the Dimensions of an Array
- •Naming Array Constants
- •Working with Array Formulas
- •Using Multicell Array Formulas
- •Using Single-Cell Array Formulas
- •Working with Multicell Array Formulas
- •What Is a Chart?
- •Understanding How Excel Handles Charts
- •Creating a Chart
- •Working with Charts
- •Understanding Chart Types
- •Learning More
- •Selecting Chart Elements
- •User Interface Choices for Modifying Chart Elements
- •Modifying the Chart Area
- •Modifying the Plot Area
- •Working with Chart Titles
- •Working with a Legend
- •Working with Gridlines
- •Modifying the Axes
- •Working with Data Series
- •Creating Chart Templates
- •Learning Some Chart-Making Tricks
- •About Conditional Formatting
- •Specifying Conditional Formatting
- •Conditional Formats That Use Graphics
- •Creating Formula-Based Rules
- •Working with Conditional Formats
- •Sparkline Types
- •Creating Sparklines
- •Customizing Sparklines
- •Specifying a Date Axis
- •Auto-Updating Sparklines
- •Displaying a Sparkline for a Dynamic Range
- •Using Shapes
- •Using SmartArt
- •Using WordArt
- •Working with Other Graphic Types
- •Using the Equation Editor
- •Customizing the Ribbon
- •About Number Formatting
- •Creating a Custom Number Format
- •Custom Number Format Examples
- •About Data Validation
- •Specifying Validation Criteria
- •Types of Validation Criteria You Can Apply
- •Creating a Drop-Down List
- •Using Formulas for Data Validation Rules
- •Understanding Cell References
- •Data Validation Formula Examples
- •Introducing Worksheet Outlines
- •Creating an Outline
- •Working with Outlines
- •Linking Workbooks
- •Creating External Reference Formulas
- •Working with External Reference Formulas
- •Consolidating Worksheets
- •Understanding the Different Web Formats
- •Opening an HTML File
- •Working with Hyperlinks
- •Using Web Queries
- •Other Internet-Related Features
- •Copying and Pasting
- •Copying from Excel to Word
- •Embedding Objects in a Worksheet
- •Using Excel on a Network
- •Understanding File Reservations
- •Sharing Workbooks
- •Tracking Workbook Changes
- •Types of Protection
- •Protecting a Worksheet
- •Protecting a Workbook
- •VB Project Protection
- •Related Topics
- •Using Excel Auditing Tools
- •Searching and Replacing
- •Spell Checking Your Worksheets
- •Using AutoCorrect
- •Understanding External Database Files
- •Importing Access Tables
- •Retrieving Data with Query: An Example
- •Working with Data Returned by Query
- •Using Query without the Wizard
- •Learning More about Query
- •About Pivot Tables
- •Creating a Pivot Table
- •More Pivot Table Examples
- •Learning More
- •Working with Non-Numeric Data
- •Grouping Pivot Table Items
- •Creating a Frequency Distribution
- •Filtering Pivot Tables with Slicers
- •Referencing Cells within a Pivot Table
- •Creating Pivot Charts
- •Another Pivot Table Example
- •Producing a Report with a Pivot Table
- •A What-If Example
- •Types of What-If Analyses
- •Manual What-If Analysis
- •Creating Data Tables
- •Using Scenario Manager
- •What-If Analysis, in Reverse
- •Single-Cell Goal Seeking
- •Introducing Solver
- •Solver Examples
- •Installing the Analysis ToolPak Add-in
- •Using the Analysis Tools
- •Introducing the Analysis ToolPak Tools
- •Introducing VBA Macros
- •Displaying the Developer Tab
- •About Macro Security
- •Saving Workbooks That Contain Macros
- •Two Types of VBA Macros
- •Creating VBA Macros
- •Learning More
- •Overview of VBA Functions
- •An Introductory Example
- •About Function Procedures
- •Executing Function Procedures
- •Function Procedure Arguments
- •Debugging Custom Functions
- •Inserting Custom Functions
- •Learning More
- •Why Create UserForms?
- •UserForm Alternatives
- •Creating UserForms: An Overview
- •A UserForm Example
- •Another UserForm Example
- •More on Creating UserForms
- •Learning More
- •Why Use Controls on a Worksheet?
- •Using Controls
- •Reviewing the Available ActiveX Controls
- •Understanding Events
- •Entering Event-Handler VBA Code
- •Using Workbook-Level Events
- •Working with Worksheet Events
- •Using Non-Object Events
- •Working with Ranges
- •Working with Workbooks
- •Working with Charts
- •VBA Speed Tips
- •What Is an Add-In?
- •Working with Add-Ins
- •Why Create Add-Ins?
- •Creating Add-Ins
- •An Add-In Example
- •System Requirements
- •Using the CD
- •What’s on the CD
- •Troubleshooting
- •The Excel Help System
- •Microsoft Technical Support
- •Internet Newsgroups
- •Internet Web sites
- •End-User License Agreement
CHAPTER
Creating Formulas
That Look Up Values
This chapter discusses various techniques that you can use to look up a value in a range of data. Excel has three functions (LOOKUP, VLOOKUP, and HLOOKUP) designed for this task, but you may find
that these functions don’t quite cut it.
This chapter provides many lookup examples, including alternative techniques that go well beyond the Excel program’s normal lookup capabilities.
Introducing Lookup Formulas
A lookup formula essentially returns a value from a table by looking up another related value. A common telephone directory provides a good analogy. If you want to find a person’s telephone number, you first locate the name (look it up) and then retrieve the corresponding number.
IN THIS CHAPTER
An introduction to formulas that look up values in a table
An overview of the worksheet functions used to perform lookups
Basic lookup formulas
More sophisticated lookup formulas
Note
I use the term table to describe a rectangular range of data. The range does not necessarily need to be an “official” table, as created by choosing Insert Tables Table. n
Figure 14.1 shows a worksheet that uses several lookup formulas. This worksheet contains a table of employee data, beginning in row 7. This range is named EmpData. When you enter a last name into cell C2, lookup formulas in D2:G2 retrieve the matching information from the table. If the last name does not appear in Column C, the formulas return #N/A.
309
Part II: Working with Formulas and Functions
About This Chapter’s Examples
Most of the examples in this chapter use named ranges for function arguments. When you adapt these formulas for your own use, you need to substitute the actual range address or a range name defined in your workbook.
|
The following lookup formulas use the VLOOKUP function: |
|
|
D2 |
=VLOOKUP(C2,EmpData,2,FALSE) |
|
|
E2 |
=VLOOKUP(C2,EmpData,3,FALSE) |
|
|
F2 |
=VLOOKUP(C2,EmpData,4,FALSE) |
|
|
G2 |
=VLOOKUP(C2,EmpData,5,FALSE) |
|
|
FIGURE 14.1
Lookup formulas in row 2 look up the information for the employee name in cell C2.
This particular example uses four formulas to return information from the EmpData range. In many cases, you want only a single value from the table, so use only one formula.
Functions Relevant to Lookups
Several Excel functions are useful when writing formulas to look up information in a table. Table 14.1 lists and describes these functions.
310
Chapter 14: Creating Formulas That Look Up Values
TABLE 14.1
|
Functions Used in Lookup Formulas |
Function |
Description |
|
|
CHOOSE |
Returns a specific value from a list of values supplied as arguments. |
|
|
HLOOKUP |
Horizontal lookup. Searches for a value in the top row of a table and returns a value in the |
|
same column from a row you specify in the table. |
|
|
IF |
Returns one value if a condition you specify is TRUE, and returns another value if the condi- |
|
tion is FALSE. |
|
|
IFERROR* |
If the first argument returns an error, the second argument is evaluated and returned. If the first |
|
argument does not return an error, then it is evaluated and returned. |
|
|
INDEX |
Returns a value (or the reference to a value) from within a table or range. |
|
|
LOOKUP |
Returns a value either from a one-row or one-column range. Another form of the LOOKUP |
|
function works like VLOOKUP but is restricted to returning a value from the last column of a |
|
range. |
|
|
MATCH |
Returns the relative position of an item in a range that matches a specified value. |
|
|
OFFSET |
Returns a reference to a range that is a specified number of rows and columns from a cell or |
|
range of cells. |
|
|
VLOOKUP |
Vertical lookup. Searches for a value in the first column of a table and returns a value in the |
|
same row from a column you specify in the table. |
|
|
* Introduced in Excel 2007.
The examples in this chapter use the functions listed in Table 14.1.
Using the IF Function for Simple Lookups
The IF function is very versatile and is often suitable for simple decision-making problems. The accompanying figure shows a worksheet with student grades in column B. Formulas in column C use the IF function to return text: either Pass (a score of 65 or higher) or Fail (a score below 65). For example, the formula in cell C2 is
=IF(B2>=65,”Pass”,”Fail”)
continued
311
Part II: Working with Formulas and Functions
continued
You can “nest” IF functions to provide even more decision-making ability. This formula, for example, returns one of four strings: Excellent, Very Good, Fair, or Poor.
=IF(B2>=90,”Excellent”,IF(B2>=70,”Very Good”,IF(B2>=50,”Fair”,”Poor”)))
This technique is fine for situations that involve only a few choices. However, using nested IF functions can quickly become complicated and unwieldy. The lookup techniques described in this chapter usually provide a much better solution.
Basic Lookup Formulas
You can use the Excel basic lookup functions to search a column or row for a lookup value to return another value as a result. Excel provides three basic lookup functions: HLOOKUP, VLOOKUP, and LOOKUP. In addition, the MATCH and INDEX functions are often used together to return a cell or relative cell reference for a lookup value.
The VLOOKUP function
The VLOOKUP function looks up the value in the first column of the lookup table and returns the corresponding value in a specified table column. The lookup table is arranged vertically (which explains the V in the function’s name). The syntax for the VLOOKUP function is
VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)
The VLOOKUP function’s arguments are as follows:
•lookup_value: The value to be looked up in the first column of the lookup table.
•table_array: The range that contains the lookup table.
•col_index_num: The column number within the table from which the matching value is returned.
•range_lookup: Optional. If TRUE or omitted, an approximate match is returned. (If an exact match is not found, the next largest value that is less than lookup_value is returned.) If FALSE, VLOOKUP will search for an exact match. If VLOOKUP can’t find an exact match, the function returns #N/A.
Caution
If the range_lookup argument is TRUE or omitted, the first column of the lookup table must be in ascending order. If lookup_value is smaller than the smallest value in the first column of table_array, VLOOKUP returns #N/A. If the range_lookup argument is FALSE, the first column of the lookup table need not be in ascending order. If an exact match is not found, the function returns #N/A.
312
Chapter 14: Creating Formulas That Look Up Values
Tip
If the lookup_value argument is text and the range_lookup argument is False, the lookup_value can include wildcard characters * and ?.
A very common use for a lookup formula involves an income tax rate schedule (see Figure 14.2). The tax rate schedule shows the income tax rates for various income levels. The following formula (in cell B3) returns the tax rate for the income in cell B2:
=VLOOKUP(B2,D2:F7,3)
On the CD
The examples in this section are available on the companion CD-ROM. They’re contained in a file named basic lookup examples.xlsx.
FIGURE 14.2
Using VLOOKUP to look up a tax rate.
The lookup table resides in a range that consists of three columns (D2:F7). Because the last argument for the VLOOKUP function is 3, the formula returns the corresponding value in the third column of the lookup table.
Note that an exact match is not required. If an exact match is not found in the first column of the lookup table, the VLOOKUP function uses the next largest value that is less than the lookup value. In other words, the function uses the row in which the value you want to look up is greater than or equal to the row value but less than the value in the next row. In the case of a tax table, this is exactly what you want to happen.
The HLOOKUP function
The HLOOKUP function works just like the VLOOKUP function except that the lookup table is arranged horizontally instead of vertically. The HLOOKUP function looks up the value in the first row of the lookup table and returns the corresponding value in a specified table row.
313
Part II: Working with Formulas and Functions
The syntax for the HLOOKUP function is
HLOOKUP(lookup_value,table_array,row_index_num,range_lookup)
The HLOOKUP function’s arguments are as follows
•lookup_value: The value to be looked up in the first row of the lookup table.
•table_array: The range that contains the lookup table.
•row_index_num: The row number within the table from which the matching value is returned.
•range_lookup: Optional. If TRUE or omitted, an approximate match is returned. (If an exact match is not found, the next largest value less than lookup_value is returned.) If FALSE, VLOOKUP will search for an exact match. If VLOOKUP can’t find an exact match, the function returns #N/A.
Tip
If the lookup_value argument is text and the range_lookup argument is False, the lookup_value can include wildcard characters * and?.
Figure 14.3 shows the tax rate example with a horizontal lookup table (in the range E1:J3). The formula in cell B3 is
=HLOOKUP(B2,E1:J3,3)
FIGURE 14.3
Using HLOOKUP to look up a tax rate.
The LOOKUP function
The LOOKUP function looks in a one-row or one-column range (lookup_vector) for a value (lookup_ value) and returns a value from the same position in a second one-row or one-column range (result_vector).
The LOOKUP function has the following syntax:
LOOKUP(lookup_value,lookup_vector,result_vector)
314
Chapter 14: Creating Formulas That Look Up Values
The function’s arguments are as follows:
•lookup_value: The value to be looked up in the lookup_vector.
•lookup_vector: A single-column or single-row range that contains the values to be looked up. These values must be in ascending order.
•result_vector: The single-column or single-row range that contains the values to be returned. It must be the same size as the lookup_vector.
Caution
Values in the lookup_vector must be in ascending order. If lookup_value is smaller than the smallest value in lookup_vector, LOOKUP returns #N/A.
Figure 14.4 shows the tax table again. This time, the formula in cell B3 uses the LOOKUP function to return the corresponding tax rate. The formula in cell B3 is
=LOOKUP(B2,D2:D7,F2:F7)
Caution
If the values in the first column are not arranged in ascending order, the LOOKUP function may return an incorrect value. n
Note that LOOKUP (as opposed to VLOOKUP) requires two range references (a range to be looked in, and a range that contains result values). VLOOKUP, on the other hand, uses a single range for the lookup table, and the third argument determines which column to use for the result. This argument, of course, can consist of a cell reference.
FIGURE 14.4
Using LOOKUP to look up a tax rate.
315
Part II: Working with Formulas and Functions
Combining the MATCH and INDEX functions
The MATCH and INDEX functions are often used together to perform lookups. The MATCH function returns the relative position of a cell in a range that matches a specified value. The syntax for
MATCH is
MATCH(lookup_value,lookup_array,match_type)
The MATCH function’s arguments are as follows:
•lookup_value: The value you want to match in lookup_array. If match_type is 0 and the lookup_value is text, this argument can include wildcard characters * and ?
•lookup_array: The range being searched.
•match_type: An integer (–1, 0, or 1) that specifies how the match is determined.
Note
If match_type is 1, MATCH finds the largest value less than or equal to lookup_value. (lookup_array must be in ascending order.) If match_type is 0, MATCH finds the first value exactly equal to lookup_value. If match_type is –1, MATCH finds the smallest value greater than or equal to lookup_value. (lookup_array must be in descending order.) If you omit the match_type argument, this argument is assumed to be 1.
The INDEX function returns a cell from a range. The syntax for the INDEX function is
INDEX(array,row_num,column_num)
The INDEX function’s arguments are as follows:
•array: A range
•row_num: A row number within array
•col_num: A column number within array
Note
If array contains only one row or column, the corresponding row_num or column_num argument is optional. n
Figure 14.5 shows a worksheet with dates, day names, and amounts in columns D, E, and F. When you enter a date in cell B1, the following formula (in cell B2) searches the dates in column D and returns the corresponding amount from column F. The formula in cell B2 is
=INDEX(F2:F21,MATCH(B1,D2:D21,0))
To understand how this formula works, start with the MATCH function. This function searches the range D2:D21 for the date in cell B1. It returns the relative row number where the date is found. This value is then used as the second argument for the INDEX function. The result is the corresponding value in F2:F21.
316
Chapter 14: Creating Formulas That Look Up Values
FIGURE 14.5
Using the INDEX and MATCH functions to perform a lookup.
When a Blank Is Not a Zero
The Excel lookup functions treat empty cells in the result range as zeros. The worksheet in the accompanying figure contains a two-column lookup table, and this formula looks up the name in cell B1 and returns the corresponding amount:
=VLOOKUP(B1,D2:E8,2)
Note that the Amount cell for Charlie is blank, but the formula returns a 0.
continued
317