Вы находитесь на странице: 1из 8

7/3/13

Using the VLookup Function in Excel 2007

United States (English)

Sign in

Dev Center

Home Products Support Forums

Office 365

Docs

Samples

Downloads

Training

How To

Office and SharePoint development > Office client development > Office 2007 > Excel 2007 > Visual How Tos > Using Share This the VLookup Function in Excel 2007

Office and SharePoint development Office client development Office 2007 Excel 2007 Visual How Tos Adding Chart Elements to Charts in Excel 2007 Consuming Web Services in Excel 2007 Creating a Master Detail View in Excel 2007 Creating Auto-Filtered Tables in Excel 2007 Creating Basic Column Charts in Excel 2007 Creating Calculated Columns in Excel 2007 Creating PivotTables In Excel 2007 Displaying Open XML Spreadsheet Tables in a Browser by Using Silverlight Merging Simple Content from Excel 2007 Workbooks and Worksheets by Using the Open XML SDK 2.0 for Microsoft Office Office Open XML Formats:

Using the VLookup Function in Excel 2007


Office 2007

Summary: Learn how to dynamically search table arrays in Microsoft Office Excel 2007 by using the built-in VLOOKUP function.

Applies to: 2007 Microsoft Office System, Microsoft Office Excel 2007, Microsoft Visual Basic for Applications (VBA) Michael LaRosa, Murphy & Associates April 2009

Often cells in a worksheet have dependencies on other cells in the same or in a separate, but related, worksheet. The most robust method of assigning values to these dependent rows is to dynamically locate and resolve these values. Office Excel 2007 contains a number of search functions to address this need. The VLOOKUP function enables you to search for a value in the first column of a table array, and if a match is found, then return a value from another column in this same row. Code It | Read It | Explore It

Watch the Video Length: 06:54 | Size: 24.6 MB | Type: WMV file

msdn.microsoft.com/en-us/library/office/dd797422%28v=office.12%29.aspx

1/8

7/3/13

Using the VLookup Function in Excel 2007

Inserting Values into Excel 2007 Cells Office Open XML Formats: Retrieving Excel 2007 Cell Values Office Open XML Formats: Retrieving Lists of Excel 2007 Worksheets Publishing Excel 2007 Workbooks to SharePoint Server 2007 Saving Workbooks to PDF and XPS Formats in Excel 2007 Setting Conditional Formatting in Excel 2007 Using LINQ to Query Tables in Excel 2007 Using the VLookup Function in Excel 2007

Code It
In this example, you create three calculated columns using the VLOOKUP function in a variety of ways. It uses information about students at a hypothetical state university.

Open and Examine StudentInfo


Download the sample file In this example you will work with the file StudentInfo.xlsx, which you can download from Code Gallery. Open this file in Office Excel 2007. It contains a workbook with three worksheets: Students contains key information about registered students. This is the worksheet that you will be modifying. GPA lists the students' quarterly and averaged yearly grade point average, on a scale from 0 to 4.0. Grades contains a GPA-to-letter conversion table. (It also contains some UI at the bottom that can be used in a procedure for calculating a grade from a GPA value. This is discussed in the section Using VLOOKUP from VBA.) This workbook represents a sample of an actual workbook that could potentially contain information about thousands or tens of thousands of students.

Adding a Buddy Email Column

The university pairs all incoming freshman with one another in a "buddy" system. Currently in the Students worksheet, the Class Buddy column lists the ID of the corresponding student. Although this information is available via a manual search in this same table, to enable easier access, another column with the buddy's email will be added. To accomplish this, you will use the VLOOKUP function to search in the same table using an exact match. To create the buddy email column 1. On the Students worksheet, enter "CB Email" into cell I1. Increase the width of this column to about 180 pixels. 2. Enter the following formula into cell I2:

= V L O O K U P ( H 2 ,A 2 : H 2 3 ,7 ,F A L S E ) Notice how the autocomplete and tooltip features in Office Excel 2007 assist in creating this formula. The parameters have the following meaning: H 2is the cell containing the value (391885) to lookup.
msdn.microsoft.com/en-us/library/office/dd797422%28v=office.12%29.aspx 2/8

7/3/13

Using the VLookup Function in Excel 2007

A 2 : H 2 3is

the table array to search through. Note the first, left-most column, here A ( ID) , represents the column to search for our lookup value.
7is

the index of the column whose cell contains the returned value of the function, assuming a match is found for the lookup value.
F A L S Especifies

performed.

that an exact search should be

When you click Enter , I2 should display the calculated value of steventh@stateu.edu. Steven Thorpe is the assigned buddy of Anne Hellung. 3. Edit the formula in I2 so that the table array uses absolute cell references:

= V L O O K U P ( H 2 ,$ A $ 2 : $ H $ 2 3 ,7 ,F A L S E ) This should not change the calculated value in I2, but is required for the next step. 4. Copy the formula in cell I2 to cells I3 through I23. The easiest way to accomplish this is to autofill this column by dragging the lower-right handle on I2 down to I23. The rest of the calculated buddy email addresses should now display. Click several of these cells to examine the underlying LOOKUP formula.

Adding a Column to Confirm Buddy Compatibility

Buddies are usually required to be in the same graduation class. To help monitor this proscription, a new column is requested to keep track of this relationship. Again you will use VLOOKUP to search in the same table using an exact match, but the results will then be used in a further calculation. To create the paired class validation column 1. On the Students worksheet, enter "CBSY?" into cell J1. This acronym represents "Class Buddies the Same Year?" 2. Enter the following formula into cell J2:

= E X A C T ( D 2 ,V L O O K U P ( H 2 ,A : H ,4 ,F A L S E ) ) This formula compares the graduation class of the current student with that of his buddy and returns the Boolean result. Note that this time that the table is described by a
msdn.microsoft.com/en-us/library/office/dd797422%28v=office.12%29.aspx 3/8

7/3/13

Using the VLookup Function in Excel 2007

range of columns, A : H . This is more appropriate for tables in which rows are dynamically added and deleted. 3. Copy this formula to the rest of the cells in this column, cells J3 through J23. Note that two paired students, Mr. White and Mr. Xie, are in different graduation classes.

Adding a Grade Column

Lastly, a request has been received to extend this worksheet with the current student average grade. To accomplish this, you perform a nested lookup using tables on the other worksheets. To create the grade column 1. On the Students worksheet, enter "Grade" into cell K1. 2. Select the column and format the cells as a Number type with 2 of precision. 3. Enter the following formula into cell K2:

= V L O O K U P ( A 2 , G P A ! A : F , 6 , F A L S E )

This formula performs a lookup using the current student's ID on the ta GPA worksheet. It returns the matching values in column 6, which is th GPA for the year.

4. Next convert this GPA into a grade by wrapping this function call inside call to VLOOKUP:

= V L O O K U P ( V L O O K U P ( A 2 , G P A ! A : F , 6 , F A L S E ) ,G r a d e s C n v ,2 ,

A lookup is performed using the result of the previous lookup, searchin table in the GPA worksheet, and returning the sixth column. Notice the The named range G r a d e s C n vis used instead of G r a d e s ! A 2 : B 1 2 shorthand convenience.

The search type is approximate (F A L S E ) and therefore the first colum the Grades worksheet must be ordered ascending.

5. Copy this formula to the rest of the cells in this column, cells K3 throug

The Resulting Students Worksheet

After you have added these three new columns, the resulting Students worksheet should appear as in Figure 1 (except that in the figure, columns D-G have been hidden to conserve space). Figure 1. Students worksheet after column additions.
msdn.microsoft.com/en-us/library/office/dd797422%28v=office.12%29.aspx 4/8

7/3/13

Using the VLookup Function in Excel 2007

Read It
The built-in VLOOKUP function in Office Excel 2007 is used to dynamically search for matching values in a table arrays. Given a value, it performs a vertical search in the first column of the specified table, and if it finds a match, it returns a value from the specified column in the same row. This function can be used to find information in a single worksheet or, more commonly, to use a known value in one worksheet to search for associated data in a row of another, related worksheet. Office Excel 2007 contains a number of related search functions, including: Function LOOKUP Description Searches for an exact matching value and, if successful, returns an associated value. The LOOKUP function has two syntax forms: the vector form and the array form. This horizontal lookup function is the counterpart to VLOOKUP. It searches for a value in the first row of a table array, and if a match is found, then it returns a value from the specified row in this
5/8

HLOOKUP

msdn.microsoft.com/en-us/library/office/dd797422%28v=office.12%29.aspx

7/3/13

Using the VLookup Function in Excel 2007

same column. MATCH Returns the relative position of an item in an array that matches a specified value in a specified order. Use MATCH instead of one of the LOOKUP functions when you need the position of an item in a range instead of the item itself.

In addition, it is common to use other built-in functionssuch as INDEX, OFFSET, FIND, SEARCH, and CHOOSEto refine searches.

Syntax

VLOOKUP has the following syntax:

V L O O K U P ( l o o k u p _ v a l u e , t a b l e _ a r r a y , c o l _ i n d e x _ n u m , [ r a n g e _

Where:
l o o k u p _ v a l u eis

the value to search for in the first column of the table array. This parameter can hold a value or a reference. Note: A text match is not case sensitive, but does consider whitespace, non-printing and special characters. You can use the TRIM or CLEAN functions to remove these characters and EXACT to compare case. Exact text searches can use the question mark (?) and asterisk (*) wildcard characters to match a single character or any sequence of characters, respectively. Exact matches on floating point values may not succeed because of rounding approximations. Instead, perform an approximate match or use the TRUNC function.

Error Conditions

The following conditions will result in error values being returned: If l o o k u p _ v a l u eis smaller than the smallest value in the first column of table_array, VLOOKUP returns # N / A . If c o l _ i n d e x _ n u mis less than 1, then # V A L U E !is returned; if greater than the number of columns in t a b l e _ a r r a y , then # R E F !is returned. If a match is not found, # N / Ais returned.

Using VLOOKUP from VBA


msdn.microsoft.com/en-us/library/office/dd797422%28v=office.12%29.aspx

The VLOOKUP function is exposed in the Office Excel 2007


6/8

7/3/13

Using the VLookup Function in Excel 2007

object model as the WorksheetFunction.VLookup method. For example, the following code could be added to the Grades worksheet to perform a basic GPA-to-grade conversion. This code utilizes the existing UI elements already found in this worksheet: the button named GradeCalc, and named cells GpaInput and GradeOutput. Note: VBA code will only function in macro-enabled spreadsheets and templates. If you want to add and test this code, save the companion file StudentInfo.xlsx as a macroenabled spreadsheet named StudentInfo.xlsm.
VB

'E v e n th a n d l e rt oc a l c u l a t eag r a d ef r o maG P A S u bG r a d e C a l c _ C l i c k ( ) D i mo G r a d e C e l lA sR a n g e S e to G r a d e C e l l=R a n g e ( " G r a d e O u t p u t " ) o G r a d e C e l l . V a l u e=" " D i mr e sA sV a r i a n t D i me r r N u mA sI n t e g e r ' T r a p sa n dr e p o r t sa l la p p l i c a t i o ne r r o r s O nE r r o rR e s u m eN e x t r e s=A p p l i c a t i o n . W o r k s h e e t F u n c t i o n . V L o o k u p ( R a n g e ( R a n g e ( " A 2 " ," B 1 2 " ) ,2 ,T r u e ) e r r N u m=E r r . N u m b e r I fe r r N u m< >0T h e n r e s=" E r r o r :"&e r r N u m E n dI f o G r a d e C e l l . V a l u e=r e s E n dS u b

This code must be implemented as an event procedure for the GradeCalc button. To create an event procedure for an existing form control 1. Right-click on the control (here the GradeCalc button) and choose the Assign Macro command from the context menu. The Assign Macro dialog box should display. 2. In the Macro Name text box, enter a name for the VBA event procedure (here G r a d e C a l c _ C l i c k ) and click New. The dialog box closes and the Visual Basic Editor opens, displaying the VBA source code for the newly created event procedure. In addition to the standard runtime errors, such as out of memory, described in Core Visual Basic Language Errors, the
msdn.microsoft.com/en-us/library/office/dd797422%28v=office.12%29.aspx 7/8

7/3/13

Using the VLookup Function in Excel 2007

VLOOKUP method also issues the application-defined error (error number 1004) for the error conditions described in the previous section.

Explore It
VLOOKUP WorksheetFunction.VLookup Method Lookup and Reference Functions Use HLookup and VLookup functions to find records in large worksheets Dynamic searching using VLOOKUP, MATCH and INDEX XL: How to Perform a Case-Sensitive Lookup The VLOOKUP and HLOOKUP functions fail to find a number in a list in Excel

Did you find this helpful?

Yes

No

Community Additions

ADD

OFFICE 2013 New features and capabilities Apps for Office Office 2013 training Try Office 365 ProPlus Preview

OFFICE EXTENSIBILITY Apps for Office Visual Studio (VSTO) Open XML Fluent UI VBA

RESOURCES Office 2010 developer map Office 2007 developer map SDKs and references Training Videos

CONNECT Apps for Office and SharePoint blog Office developer blog Office developer forums Microsoft community Stack Overflow

DEVELOPER CENTERS Apps for Office and SharePoint SharePoint Exchange Lync Visual Studio

INFORMATION FOR Office 365 Decision makers IT pros Developers Business/home

Manage Your Profile Newsletter Privacy Statement Terms of Use Trademarks Site Feedback 2013 Microsoft

msdn.microsoft.com/en-us/library/office/dd797422%28v=office.12%29.aspx

8/8

Вам также может понравиться