You are on page 1of 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

It uses information about students at a hypothetical state university.) This workbook represents a sample of an actual workbook that could potentially contain information about thousands or tens of thousands of students.com/en-us/library/office/dd797422%28v=office. to enable easier access. on a scale from 0 to 4. Currently in the Students worksheet. Although this information is available via a manual search in this same table. another column with the buddy's email will be added. Adding a Buddy Email Column The university pairs all incoming freshman with one another in a "buddy" system. the Class Buddy column lists the ID of the corresponding student. On the Students worksheet. 2.aspx 2/8 .A 2 : H 2 3 .xlsx.0.7 . Increase the width of this column to about 180 pixels. (It also contains some UI at the bottom that can be used in a procedure for calculating a grade from a GPA value. you create three calculated columns using the VLOOKUP function in a variety of ways. Enter the following formula into cell I2: = V L O O K U P ( H 2 . This is the worksheet that you will be modifying. Open and Examine StudentInfo Download the sample file In this example you will work with the file StudentInfo. This is discussed in the section Using VLOOKUP from VBA. The parameters have the following meaning: H 2is the cell containing the value (391885) to lookup. msdn. It contains a workbook with three worksheets: Students contains key information about registered students. Grades contains a GPA-to-letter conversion table.F A L S E ) Notice how the autocomplete and tooltip features in Office Excel 2007 assist in creating this formula. To create the buddy email column 1. enter "CB Email" into cell I1. GPA lists the students' quarterly and averaged yearly grade point average. Open this file in Office Excel 2007. you will use the VLOOKUP function to search in the same table using an exact match.microsoft. which you can download from Code Gallery. To accomplish this.12%29.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.

To create the paired class validation column 1. To help monitor this proscription. enter "CBSY?" into cell J1. The easiest way to accomplish this is to autofill this column by dragging the lower-right handle on I2 down to I23. On the Students worksheet. Click several of these cells to examine the underlying LOOKUP formula. Note the first. The rest of the calculated buddy email addresses should now display.7 . Again you will use VLOOKUP to search in the same table using an exact match.microsoft.V L O O K U P ( H 2 . here A ( ID) .7/3/13 Using the VLookup Function in Excel 2007 A 2 : H 2 3is the table array to search through. left-most column. but is required for the next step. F A L S Especifies performed. Adding a Column to Confirm Buddy Compatibility Buddies are usually required to be in the same graduation class.A : H . I2 should display the calculated value of steventh@stateu. This acronym represents "Class Buddies the Same Year?" 2.F A L S E ) This should not change the calculated value in I2. Copy the formula in cell I2 to cells I3 through I23. represents the column to search for our lookup value.aspx 3/8 . Steven Thorpe is the assigned buddy of Anne Hellung. Enter the following formula into cell J2: = E X A C T ( D 2 . a new column is requested to keep track of this relationship.$ A $ 2 : $ H $ 2 3 . 3. Edit the formula in I2 so that the table array uses absolute cell references: = V L O O K U P ( H 2 .com/en-us/library/office/dd797422%28v=office. Note that this time that the table is described by a msdn.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. assuming a match is found for the lookup value.12%29. 4. that an exact search should be When you click Enter .edu. 7is the index of the column whose cell contains the returned value of the function. but the results will then be used in a further calculation.4 .

columns D-G have been hidden to conserve space). To create the grade column 1. A : H . Note that two paired students. 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. cells K3 throug The Resulting Students Worksheet After you have added these three new columns. 5.G r a d e s C n v .aspx 4/8 . Adding a Grade Column Lastly. 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 . On the Students worksheet. 4.12%29. Figure 1. 6 . Students worksheet after column additions. 3. Enter the following formula into cell K2: = V L O O K U P ( A 2 .7/3/13 Using the VLookup Function in Excel 2007 range of columns. G P A ! A : F . searchin table in the GPA worksheet. you perform a nested lookup using tables on the other worksheets. are in different graduation classes. Mr. msdn.com/en-us/library/office/dd797422%28v=office. and returning the sixth column. Xie.microsoft. which is th GPA for the year.2 . Copy this formula to the rest of the cells in this column. A lookup is performed using the result of the previous lookup. G P A ! A : F . The search type is approximate (F A L S E ) and therefore the first colum the Grades worksheet must be ordered ascending. F A L S E ) . F A L S E ) This formula performs a lookup using the current student's ID on the ta GPA worksheet. 3. It returns the matching values in column 6. Copy this formula to the rest of the cells in this column. cells J3 through J23. Select the column and format the cells as a Number type with 2 of precision. 2. White and Mr. To accomplish this. enter "Grade" into cell K1. This is more appropriate for tables in which rows are dynamically added and deleted. a request has been received to extend this worksheet with the current student average grade. 6 . the resulting Students worksheet should appear as in Figure 1 (except that in the figure.

then it returns a value from the specified row in this 5/8 HLOOKUP msdn.12%29. it performs a vertical search in the first column of the specified table. This function can be used to find information in a single worksheet or. It searches for a value in the first row of a table array. to use a known value in one worksheet to search for associated data in a row of another.aspx . This horizontal lookup function is the counterpart to VLOOKUP.microsoft. if successful. related worksheet. The LOOKUP function has two syntax forms: the vector form and the array form. more commonly. Given a value. and if a match is found.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. including: Function LOOKUP Description Searches for an exact matching value and.com/en-us/library/office/dd797422%28v=office. it returns a value from the specified column in the same row. returns an associated value. and if it finds a match. Office Excel 2007 contains a number of related search functions.

microsoft. 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. and CHOOSE—to refine searches. c o l _ i n d e x _ n u m . You can use the TRIM or CLEAN functions to remove these characters and EXACT to compare case. Exact matches on floating point values may not succeed because of rounding approximations. Using VLOOKUP from VBA msdn. If c o l _ i n d e x _ n u mis less than 1. then # V A L U E !is returned. # N / Ais returned. MATCH Returns the relative position of an item in an array that matches a specified value in a specified order. respectively. Note: A text match is not case sensitive. then # R E F !is returned. VLOOKUP returns # N / A . Instead.aspx The VLOOKUP function is exposed in the Office Excel 2007 6/8 . FIND. but does consider whitespace. [ 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. t a b l e _ a r r a y . non-printing and special characters. it is common to use other built-in functions—such as INDEX.12%29. SEARCH.7/3/13 Using the VLookup Function in Excel 2007 same column. This parameter can hold a value or a reference. OFFSET. Syntax VLOOKUP has the following syntax: V L O O K U P ( l o o k u p _ v a l u e . In addition. Exact text searches can use the question mark (?) and asterisk (*) wildcard characters to match a single character or any sequence of characters.com/en-us/library/office/dd797422%28v=office. If a match is not found. 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. if greater than the number of columns in t a b l e _ a r r a y .

W o r k s h e e t F u n c t i o n . 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 . and named cells GpaInput and GradeOutput. Note: VBA code will only function in macro-enabled spreadsheets and templates. Right-click on the control (here the GradeCalc button) and choose the Assign Macro command from the context menu. In addition to the standard runtime errors. In the Macro Name text box. save the companion file StudentInfo.microsoft. the msdn. To create an event procedure for an existing form control 1.xlsm. The dialog box closes and the Visual Basic Editor opens. V L o o k u p ( R a n g e ( R a n g e ( " A 2 " ." B 1 2 " ) .2 .7/3/13 Using the VLookup Function in Excel 2007 object model as the WorksheetFunction. 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 . the following code could be added to the Grades worksheet to perform a basic GPA-to-grade conversion. If you want to add and test this code.VLookup method. 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 . 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. 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.com/en-us/library/office/dd797422%28v=office. displaying the VBA source code for the newly created event procedure.xlsx as a macroenabled spreadsheet named StudentInfo.T r u e ) e r r N u m=E r r . described in Core Visual Basic Language Errors. 2. This code utilizes the existing UI elements already found in this worksheet: the button named GradeCalc. such as out of memory. For example.aspx 7/8 . The Assign Macro dialog box should display.12%29.

com/en-us/library/office/dd797422%28v=office.microsoft.aspx 8/8 .12%29. Explore It VLOOKUP WorksheetFunction.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.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.