Microsoft Office Excel 2007 Lookup and Logic Functions Course Material Created By: Kyle Gerber Brought to you by: Hawkeye Community College Business & Industry Services VLOOKUP and HLOOKUP VLOOKUP (Vertical Lookup) and HLOOKUP (Horizontal Lookup) are very similar functions so they will be discussed in the same section. These two functions will look in the leftmost column or topmost row of a range of cells and find a specific value. After the value is found they will return the value of a cell in the same column or row that is a specified distance away. The parameters for these functions are as follows, beginning with VLOOKUP. VLOOKUP Explained: VLOOKUP(lookup_value, table_array, col_index_num, range_lookup) lookup_value is the value for which you are searching in the first column of the range. table_array is two or more columns of data. The first column contains the values being searched for the lookup value. These values can be either text, numbers, or logical values (i.e. TRUE, FALSE). col_index_num is the index number of the column from which you want the value returned. The first column of the lookup range is considered column 1. Each column to the right is numbered consecutively. range_lookup specifies whether you wish to find an exact match. Setting this parameter to FALSE forces the function to find an exact match to the lookup value. If an exact match is not found an #N/A error is returned. If this value is omitted or set to TRUE the function will find the next largest value that is less than or equal to the search value. Microsoft Excel 2007 - Lookup & Logical Functions -2- HLOOKUP Explained: HLOOKUP(lookup_value, table_array, row_index, range_lookup) lookup_value is the value for which you are searching in the first row of the range. table_array is two or more rows of data. The first row contains the values being searched for the lookup value. These values can be either text, numbers, or logical values (i.e. TRUE, FALSE). row_index_num is the index number of the row from which you want the value returned. The first row of the lookup range is considered row 1. Each row below is numbered consecutively. range_lookup specifies whether you wish to find an exact match. Setting this parameter to FALSE forces the function to find an exact match to the lookup value. If an exact match is not found an #N/A error is returned. If this value is omitted or set to TRUE the function will find the next largest value that is less than or equal to the search value. Example 1 The table below shows the quarterly sales for various salesmen. Suppose in cell B3 you wish to return the sales of the salesman in B1 from the quarter in B2. This task can easily be accomplished with a VLOOKUP statement. Microsoft Excel 2007 - Lookup & Logical Functions -3- 1. Activate cell B3 and type the following (without the quotes) to begin the function: “=VLOOKUP(” 2. For the lookup_value parameter select B1 since this cell contains the name of the salesman for which we want to search. 3. For the table_array parameter select the entire range of cells encompassing all the salesmen and all the sales for all four quarters (A6:E14). 4. For the col_index_num select cell B2 and then add one to this number by typing “+1”. The “+1” is necessary because the VLOOKUP function numbers the first column in the lookup_array as column 1. This means the column containing Q1 sales is actually column 2. 5. Since we wish to find an exact match to our salesman name type “FALSE” (not case-sensitive) for the range_lookup parameter. 6. Add the ending parentheses “)” and hit enter. When you are finished the formula should look like the following: Cell B3 now contains the sales of the salesman specified in cell B1 in the quarter specified in cell B2. Microsoft Excel 2007 - Lookup & Logical Functions -4- Example 2 This example is much the same as Example 1 except it takes advantage of HLOOKUP instead of VLOOKUP. The table below shows the daily high temperatures for the first six months of the year for the first 10 days of each month. What if you wished to query this data in order to find the high temperate for a given day of a given month? The HLOOKUP function can provide this information in much the same way VLOOKUP provided information in Example 1. 1. Select cell B1 and type the following (without the quotes) to begin the function: “HLOOKUP(” 2. For the lookup_value parameter select cell B2 since this cell contains the value for which you want to search. 3. For the table_array parameter select the entire range of cells including all the month names, dates, and temperature information (A5:G15). 4. For the row_index_num parameter select cell B2 and then add one to this number by typing “+1”. Again, the +1 is necessary because the HLOOKUP function numbers the first row in the lookup_array as row 1. This makes the row containing day 1 row 2. 5. We wish to find an exact match to our lookup_value so type “FALSE” (not casesensitive) for the range_lookup parameter. Microsoft Excel 2007 - Lookup & Logical Functions -5- 6. Add the ending parentheses “)” and hit enter. When you are finished the formula should look like the following: Cell B3 now contains the daily high temperature for the month specified in cell B1 on the day specified in cell B2. Microsoft Excel 2007 - Lookup & Logical Functions -6- Example 3 Not always is it necessary to select a range of cells for the table_array parameter of the VLOOKUP or HLOOKUP functions. Sometimes, when the lookup values never change, it is acceptable to manually type them into the function. This example will demonstrate how to manually enter an array into a formula and it will also demonstrate an occasion when the range_lookup parameter should be set to TRUE. In the field of teaching it is often necessary to record a letter grade for how many points a student scored on a test. The following function will give the correct letter grade for a numeric score entered into cell B1. =HLOOKUP(B1,{0,60,63,67,70,73,77,80,83,87,90,93,97;“F”,“D”,“D”,“D+”,“C”, “C”,“C+”,“B-”,“B”,“B+”,“A-”,“A”,“A+”},2,TRUE) This function at first seems rather long and confusing so lets break it down into pieces based on what we have learned about the HLOOKUP function. The first parameter, lookup_value, is specified as cell A1 so whatever value is in cell A1 will be searched for in the first row of the lookup_array. The second parameter, lookup_array, is normally a range address such as B5:G15. In this case, however, it is a manually entered array. To manually enter an array into a function use the following conventions. o The array must be enclosed with braces {} (often called curly brackets) o Values in different columns are separated with commas {1,2,3,4} o Values in different rows are separated with semi-colons {1,2,3;11,12,13} o All text must be enclosed in quotes {1,2,3; “A”, “B”, “C”} Microsoft Excel 2007 - Lookup & Logical Functions -7- The manual array entered in the example function is the same as the table below: {0,60,63,67,70,73,77,80,83,87,90,93,97; “F”,“D-”,“D”,“D+”,“C-”,“C”,“C+”,“B-”,“B”,“B+”,“A-”,“A”,“A+”} Equals: The third parameter, row_index_num, is 2. This means the value returned will be from the 2nd row in the table_array. The fourth parameter, range_lookup, is set to TRUE which means an exact match to the lookup_value is not required. When this parameter is set to TRUE the matched value will be the next largest value that is less than or equal to the search value. With the function entered into cell B2, entering a score of 92 will give a letter grade of “A-”. Microsoft Excel 2007 - Lookup & Logical Functions -8- MATCH The match function is used to find the position of a specified value in an array or list. Unlike other lookup functions, MATCH returns the position of an item in a list instead of the item itself. It is often used in conjunction with the INDEX function in order to create a more powerful variant of VLOOKUP and HLOOKUP. This topic will be covered in the INDEX function section. MATCH has the following arguments. MATCH Explained: MATCH(lookup_value, lookup_array, match_type) lookup_value is the value for which you wish to search in the array or list. lookup_array is the range of cells or list of values which will be searched to find the lookup_value. match_type specifies how excel finds a match to the lookup_value. This parameter can be one of three values 1, 0, or -1. If match_type is 1, the function finds the largest value that is less than or equal to the lookup_value. If match_type is 0, the function finds the first value that exactly matches the lookup_value. If match_type is -1, the function finds the smallest value that is greater than or equal to the lookup_value. 1 is the default value for this parameter. If match_type is set to 0 and an exact match is not found an #N/A error will be returned. Microsoft Excel 2007 - Lookup & Logical Functions -9- Example 1 The Duggar family has 17 children. Suppose you wanted to create a spreadsheet of their names with a function that would tell you the order in which they were born. The MATCH function would be a good choice to accomplish this goal. Below is a table listing the names of all the children in chronological order. 1. In cell B2 type the following (without the quotes) to begin the function: “=MATCH(” 2. For the lookup_value parameter select cell B1 since this is the cell containing the value for which you want to search. 3. For the lookup_array parameter select the list of names beginning in cell A4 and ending in cell A20. 4. Since we want the function to find an exact match to the lookup_value set the match_type parameter to 0. Microsoft Excel 2007 - Lookup & Logical Functions - 10 - 5. Add the ending parentheses “)” and hit enter. If everything was done correctly the function should look like this: Microsoft Excel 2007 - Lookup & Logical Functions - 11 - Example 2 On its own the MATCH function has limited capabilities. It is normally used in conjunction with other functions to extend their power. This example will demonstrate how to use the MATCH function in conjunction with the VLOOKUP function. The table below shows a listing of several used cars and some information about them. Our goal is to create functions in cells B2:B7 that will return corresponding information about the car specified in cell B1. To do this we will use a VLOOKUP function but instead of entering a static value for the index_col_num parameter we will use the MATCH function. 1. Select cell B2 and enter the following (without the quotes) to begin the function. “=VLOOKUP(” 2. For the lookup_value select cell $B$1. Be sure to add the absolute cell reference signs ($) to both the row and column since we will be copying this formula down once it is completed. 3. For the table_array parameter select the entire table ($C$9:$G$21). Again, be sure to add absolute cell references. Microsoft Excel 2007 - Lookup & Logical Functions - 12 - 4. For the index_col_num enter the following MATCH formula. MATCH(A2,$9:$9,0) a. Since we are looking for the car Make in the first row of the data table the lookup_value parameter is set to A2. b. The lookup_array parameter is set to the entire row containing the data table header values. We could have set this parameter to just the range in use ($A$9:$G$9) but this way is more powerful for reasons that will be explained later. c. The match_type parameter is set to 0 since we’re looking for an exact match to the lookup_value. 5. For the range_lookup parameter of the VLOOKUP function enter FALSE to force the function to find an exact match to the lookup_value. 6. Add the ending parentheses “)” and hit enter. Assuming everything was entered correctly the finished equation should look like this: Microsoft Excel 2007 - Lookup & Logical Functions - 13 - After you hit enter Cell B2 displays the Make of car# CN005. To complete the list of lookup values drag the formula in cell B2 down to cell B7. This is where the absolute cell references are important. If the lookup values are not correct make sure you added dollar signs ($) in all the correct places throughout the function. Up to this point everything in this example could have been performed with a simple VLOOKUP. The next part of this example will demonstrate the power of using the MATCH function instead of entering static values for the col_index_num parameter. Microsoft Excel 2007 - Lookup & Logical Functions - 14 - Suppose you wanted to add several more pieces of information about the used cars in the data table. It also just so happens that you want to add the additional columns in the middle of the data table like so: To fill the empty lookup cells simply drag the formula down from cell B4 into cells B5 and B6. Since the MATCH function automatically finds the column index number, the data table can be rearranged indefinitely without disturbing the lookup cells. If static numbers were entered for the col_index_num parameter the functions would need to be modified every time a change was made to the table. Microsoft Excel 2007 - Lookup & Logical Functions - 15 - INDEX INDEX is another function that is not typically used on its own. It is usually in conjunction with other functions, primarily the MATCH function which we just finished discussing. INDEX returns a value from a range cells when the relative position of this item in the range is specified. The parameters for the INDEX function are as follows. INDEX Explained: INDEX(array, row_num, col_num) array is a range of cells containing the value which you want the function to return. row_num is a row number from which the function will return a value. The first row in the array is considered row 1. For single row ranges this parameter may be omitted. col_num is a column number from which the function will return a value. The first column of the array is considered column 1. For single column ranges this parameter may be omitted. Example 1 This example is probably not very useful when it comes to the real world but it does demonstrate how the INDEX function works. The data table below shows how much fruit in tons was consumed in several western states last year (as you can probably tell the numbers are fake). Microsoft Excel 2007 - Lookup & Logical Functions - 16 - If you wanted to know how many tons of strawberries were consumed in Montana last year you could use the index function to find out. 1. Select cell A1 and type the following (without the quotes) to begin the function. “=INDEX(” 2. For the array parameter select the entire range (A3:F10) 3. For the row_num parameter type “4” because “Strawberry” appears in the fourth row of the array. 4. For the col_num parameter type “4” because “Montana” appears in the fourth column of the array. 5. Add the ending parentheses “)” and hit enter. If everything was done correctly your function should look like this: After you hit enter the value 12 will appear in cell A1 since there were 12 tons of strawberries consumed in Montana last year. Microsoft Excel 2007 - Lookup & Logical Functions - 17 - Example 2 Based on Example 1 you’re probably doubting the usefulness of the INDEX function. You’re right, this function by itself is pointless except for a few very specialized cases. However, when you combine this function with the MATCH function you have a much more powerful version of VLOOKUP. The table below is the same as in Example 1 except the Fruit column has been moved to the right hand side. Suppose you wanted to find out how many tons of a given fruit was consumed in a given state. INDEX and MATCH can be used together to find this information. 1. Select cell B3 and enter the following (without the quotes) to begin the function. “=INDEX(” 2. For the array parameter select the entire data range (A5:F12). 3. For the row_num parameter enter the following MATCH function. MATCH(B2,F5:F12,0) a. Since we want to find the relative row of the specified fruit in the data table select B2 for the lookup_value in the MATCH function. b. The lookup_array is the range in which we are searching for the lookup_value so select the range (F5:F12). c. Since we want to find an exact match to the lookup_value enter 0 for the match_type parameter. Microsoft Excel 2007 - Lookup & Logical Functions - 18 - 4. For the col_num parameter enter the following MATCH function MATCH(B1,A5:F5,0) a. Since we want to find the relative column number of the specified state in the data table select B1 for the lookup_value in the MATCH function. b. The lookup_array is the range in which we are searching for the lookup_value so select the range (A5:F5). c. Sicne we want to find an exact match to the lookup_value enter 0 for the match_type parameter. 5. Add the ending parentheses “(” and hit enter. If everything was entered correctly the function should look like this: Cell B3 should now return “2” since 2 tons of Raspberries were consumed in Colorado. Microsoft Excel 2007 - Lookup & Logical Functions - 19 - At first this may not seem that there is much of a difference between this method and VLOOKUP but there is actually a very important distinction. This example is impossible for VLOOKUP because the lookup_value is not in the leftmost column of the data table. This is a requirement for VLOOKUP but not for INDEX and MATCH. When using these two functions the relative locations of the ranges containing the lookup values is not important. To illustrate this point, move the Fruit column between the Montana and Utah columns and move the State row between the Strawberry and Blueberry rows. As you can see the value in the look up cell B2 did not change after the data table was rearranged. Microsoft Excel 2007 - Lookup & Logical Functions - 20 - COUNT and COUNTA Though COUNT and COUNTA are technically not reference functions they are used frequently with the OFFSET function, which we will be discussing next, so we will take a moment and touch on how they are used. The COUNT function counts the number of cells containing numeric values in a specified range. It has the following arguments. COUNT Explained: COUNT(value1, value2, value3, …) value1, value2, value3 Can be either references to cells or static values (max of 30). Example 1 The following figure shows a range of cells containing numeric values, text values, and logical values. Entering the following COUNT function in cell A1 will count the number of numeric values in the range A3:B10. =COUNT(A3:B10) Microsoft Excel 2007 - Lookup & Logical Functions - 21 - COUNTA Explained: The COUNTA function works exactly the same way as COUNT except it counts the total number of non-blank cells, not just the cells containing numeric values. COUNTA has the following parameters. COUNTA(value1, value2, value3, …) value1, value2, value3 Can be either references to cells or static values (max of 30). Example 1 If we take the same range of cells used in the previous example and examine it with the COUNTA function instead of the COUNT function the total number of non-blank cells is returned. Since, in this case, there are no empty cells the function returns 16. Microsoft Excel 2007 - Lookup & Logical Functions - 22 - OFFSET The offset function is one that is rarely used but it can be very useful in the right circumstances. Unlike the other functions with which we have been working the OFFSET function returns a reference to a range of cells instead of a value from a range of cells. The reference can be either a single cell or a range of cells. OFFSET has the following parameters. OFFSET EXPLAINED: OFFSET(reference, rows, cols, height, width) reference is the base cell or range of cells off which the returned reference range is located. rows is the number of rows from the reference cell to the upper left cell of reference range. cols is the number of columns from the reference cell to the upper left cell of the reference range. height is an optional parameter that determines the number of rows in the reference range. If this parameter is omitted it is assumed to be the same height as the reference. width is an optional parameter that determines the number of columns in the reference range. If this parameter is omitted it is assumed to be the same width as the reference. Microsoft Excel 2007 - Lookup & Logical Functions - 23 - Example 1 The following table shows how much Billy made at the lemonade stand which he has opened consistently three times a month for three years. Suppose you wanted to find Billy’s total yearly income for a specified year. This can be accomplished with the OFFSET function. 1. Select cell B2 and enter the following (without the quotes) to begin the function. “=Sum(OFFSET(” 2. For the reference parameter of the OFFSET function select cell A4. 3. For the rows parameter enter the following: 5*(B1-1)+2 a. This expression evaluates to the number of rows from the reference cell to the upper left cell in the sum range for a given year. b. For example, if the year cell (B1) is set to 2: i. (B1-1) evaluates to 1 ii. 5*(B1-1) evaluates to 5 iii. 5*(B1-1)+2 evaluates to 7 c. This means the offset cell is 7 rows below the reference cell. 4. For cols parameter of the OFFSET function enter 0 because we do not want the offset cell to be shifted any columns from the reference cell. Microsoft Excel 2007 - Lookup & Logical Functions - 24 - 5. For height parameter enter 3 because Billy’s lemonade stand was open three times a month. 6. For the width parameter enter 12 for the 12 months in a year. 7. Add two ending parentheses “))”, one for the OFFSET function and one for the SUM function, and hit enter. If everything was done correctly the function should look like this: Microsoft Excel 2007 - Lookup & Logical Functions - 25 - Now, for whichever year is entered into cell A2 (1-3) the sum of all Billy’s yearly earnings will appear in cell B2. In order to understand exactly what is happening in this function the following series of figures steps through the function as it evaluates each individual parameter until it reaches the final sum. 1. Cell B1 evaluates to 2 since we want the sum of Billy’s earnings for year 2. 2. 2-1 equals 1. We need to subtract 1 from this number in order for the rows parameter to be correct. 3. 5*1 equals 5. 5 is the number of rows taken up by each “Year”. For example, from the Year 1 cell (A4) to January’s final profit (A8) is 5 rows. 4. 5+2 equals 7. We need to add 2 because the multiple of 5 just gets us from the Year 1 cell to the Year 2 cell. Adding two more gets us to January’s first profit in Year 2. 5. We are now ready to evaluate the OFFSET function. The reference cell is A4. Based on this cell we are going to move down 7 rows and 0 columns. The new range is going to be 3 rows tall and 12 columns wide. Microsoft Excel 2007 - Lookup & Logical Functions - 26 - 6. The OFFSET function returns a reference to the range A11:L13. 7. Summing all the cells in the range gives a total of $207.42. Microsoft Excel 2007 - Lookup & Logical Functions - 27 - Example 2 Another common and very powerful use for the OFFSET function is in the creation of dynamic named ranges. A named range is simply a group of cells to which you give a name. It can be any name as long as it does not contain spaces or special punctuation characters. To name a range of cells simply highlight a range of cells, place your cursor in the Name Box (see figure below), type in the desired name, and hit enter. Name Box You can also go to Formulas Tab Defined Name Define Names to open the Named Ranges dialogue. The benefit of using named ranges is that it makes it very convenient to refer to ranges that you use often. For example, if you frequently refer to the range A1:F20, you could name the range “MyRange”. From that point on you would simply type “MyRange” in formulas (i.e. =SUM(MyRange)). Adding a name in this way is fine except the range size is static. Whenever you add data to the range, the name definition has to be manually modified to account for this. Dynamic Named Ranges solve this problem. They grow and shrink according the amount of data in your range. This is possible thanks to the OFFSET function. The data table below shows sales of winter clothing offered in a sporting goods store for a given day. In cell A2 we want to create a SUM formula that will always give a total sum of all the items sold that day regardless of the list size. Microsoft Excel 2007 - Lookup & Logical Functions - 28 - 1. First we must create a dynamic named range for the Sales of all the winter items. Go to the Formulas Tab Defined Name Define Names dropdown. 2. Type “TotalSales” (without the quotes) in the name text box directly left of the Ok button. 3. Type the following OFFSET function in the “Refers to:” text box: =OFFSET($B$5,0,0,COUNTA($B:$B)-1,1) a. The reference cell is B5. b. We want the first cell of the returned range to be 0 rows and 0 cols from the reference cell. Microsoft Excel 2007 - Lookup & Logical Functions - 29 - c. The height of the returned range is determined by the COUNTA($B:$B) function which gives the total number of non-blank cells in column B. 1 is subtracted from this number to account for the Sales header row. d. The width of the returned range is 1. 4. Click OK in the Define Name dialogue. 5. Select cell A2, type “=SUM(TotalSales)” (without the quotes), and hit enter. Cell A2 now displays the total sales for the current items in the list. To prove to ourselves that the named range TotalSales is indeed dynamic delete rows 6, 7, and 8 (Mittens, Hats, Boots). Notice how the SUM function updates automatically after the rows are deleted. This is just one of the many uses for Dynamic Named Ranges. Microsoft Excel 2007 - Lookup & Logical Functions - 30 - SUMPRODUCT SUMPRODUCT is a function that is normally used to compute a grand total from two columns of data consisting of prices for individual items and quantities of items sold. It is possible, however, to use this function in a very powerful way as a lookup tool. Before describing how this function can be used to lookup values from a list we will look at how it works normally. This will set the stage for learning about the additional functionality. SUMPRODUCT has the following parameters. SUMPRODUCT Explained: SUMPRODUCT(array1, array2, array3, …) array1, array2, array3 Are 2 to 30 arrays whose contents will be multiplied together and then added. In row 1 of the arrays the value in the first column of array1 will be multiplied with the values in the first columns of array2 and array3. The value in the second column of array1 will be multiplied with the values in the second columns of array2 and array3. This will continue for all the columns of arrays 1, 2, and 3. These products will then be summed and stored in memory. In row 2 of the arrays the process is repeated where the values in the first columns of the three arrays are multiplied and then summed with the multiplication of the values in the other columns of the three arrays. In a final step, all the subtotals for each of the rows are summed together to get a grand total. All arrays must have the same dimensions. If they do not the function will return a #VALUE! error. An alternate, and possibly more readable, syntax for SUMPRODUCT is as follows. =SUMPRODUCT(array1*array2*array3*…) Either syntax will work as long as the function is being used as intended. There is no difference between the two. This syntax does give insight into some of the other possible uses for SUMPRODUCT (what if other operators were used instead of the multiplication (*) sign). Microsoft Excel 2007 - Lookup & Logical Functions - 31 - Example 1 In this first example the SUMPRODUCT function will be used to replace individual SUM and PRODUCT functions. The table below shows prices and quantities sold for different items. The Ext column subtotals the sales of each item. These subtotals are summed to obtain a Grand Total at the bottom of the list. SUMPRODUCT can replace both of these functions and eliminate the need for a Ext column if one is not desired. Microsoft Excel 2007 - Lookup & Logical Functions - 32 - Microsoft Excel 2007 - Lookup & Logical Functions - 33 - Example 2 In this example we will further our understanding by examining two seemingly similar but completely different uses of the SUMPRODUCT function. It is important to fully understand how this function works in order to effectively use it as a lookup tool. In the first table below SUMPRODUCT is used with two array parameters, columns 1,2,3 and columns 4,5,6. In the second table it is used with three array parameters, columns 1,2 columns 3,4, and columns 5,6. Notice the dramatic difference in the results shown in the third table. Microsoft Excel 2007 - Lookup & Logical Functions - 34 - The difference between these two examples is in the definition of how SUMPRODUCT works. It sums the product of the individual columns in each array. =SUMPRODUCT(A2:C6,D2:F6) is evaluated in the following way: =SUMPRODUCT(A2:B6,C2:D6,E2:F6) is evaluated like this: Now that we have the basics down we can begin to look at different ways of using this powerful function. There are two keys to understanding how SUMPRODUCT may be used in different applications. Keys to understanding the other applications of SUMPRODUCT 1. Other operators besides multiplication (*) may be used on the array parameters. These include but are not limited to =, <, >, +, -. 2. Based on the principle of coercion Excel views Boolean values TRUE and FALSE the same as 1 and 0, respectively, when performing arithmetic operations. For example: a. TRUE*Something = Something b. TRUE + 2 = 3 c. FALSE*Anything = 0 d. 3 – FALSE = 3 Microsoft Excel 2007 - Lookup & Logical Functions - 35 - Example 3 This example will look much like a VLOOKUP example and indeed it would probably be much simpler to use VLOOKUP in this case but the point is to understand how SUMPRODUCT may be used as a lookup function. The table below shows a list of salesmen and their sales figures. Our job is to enter a SUMPRODUCT formula into cell B2 that will look up the sales for the salesman name in cell B1. 1. Select cell B2 and enter the following (without the quotes) to begin the function: “=SUMPRODUCT(” 2. Enter the following and hit enter: “(A5:A11=$B$1)*B5:B11” 3. Add the ending parentheses “)” and hit enter. If everything was done correctly the function should look like this: Microsoft Excel 2007 - Lookup & Logical Functions - 36 - The function returns the correct sales for the name given in cell B1. For a relatively simple function there is quite a lot going on behind the scenes. The first step Excel takes in evaluating this function is to go through and compare all the values in the list of salesmen to the specified value in cell B1. As mentioned before this is the same as: So far we have evaluated the “(A5:A11=$B$1)” portion of the function into an array of 1’s and 0’s. Based on our original formula we still need to multiply this array with the list of sales numbers “(A5:A11=$B$1) *B5:B11”. The final step in the SUMPRODUCT function is to sum all the subtotals calculated for each row. In this case the addition is pretty easy since every value is zero except for one. =SUMPRODUCT((A5:A11=$B$1)*B5:B11) = $1,500 Microsoft Excel 2007 - Lookup & Logical Functions - 37 - Notice that the multiplication operator was used instead of a comma between our array parameters. This is a glitch in Excel. While there should be no difference between the two, the function will not work correctly if a comma is used in place of the asterisk (*). The error has to do with Excel not being able to correctly coerce the TRUE, FALSE values into numeric 1’s and 0’s. When this glitch occurs and the multiplication is performed between the two arrays, Excel sees numbers being multiplied with text values so it throws an error. So now you see how the SUMPRODUCT can be used as a lookup function. This was a fairly simple example and in this case it would have been easier to use VLOOKUP. In the next example we will see how, with the use of SUMPRODUCT, it is possible to perform lookups which would otherwise be impossible. Microsoft Excel 2007 - Lookup & Logical Functions - 38 - Example 4 The most common use for SUMPRODUCT as a lookup function is when a value needs to be returned based on multiple criteria. VLOOKUP, HLOOKUP, INDEX and MATCH are all single criteria lookup functions. The following data table shows two ranges of data. The range in columns A:E represents strain gages that are attached to a piece of machinery. Each gage has a number, Location, Material, Safety Factor (SF), and Group. It is our job to fill in the group number for each gage based on the information in the second range of data in columns G:J. This range correlates a group number for each combination of Location, Material, and SF. 1. Select cell E2 and enter the following (without the quotes) to begin the function: “=SUMPRODUCT(” Microsoft Excel 2007 - Lookup & Logical Functions - 39 - 2. Multiply the following array parameters together inside the function: a. (B2=$G$2:$G$17) b. (C2=$H$2:$H$17) c. (D2=$I$2:$I$17) d. $J$2:$J$17 e. Be sure to use absolute referencing in the indicated places when referring to ranges. This will prevent errors from occurring when the formula is filled down. 3. Add the ending parentheses “)” and hit enter. If everything was done correctly the function should look like this: 4. Drag the formula down through row 28 so every entry has a group number. The SUMPRODUCT function has searched through the lookup list and assigned the correct group number to each gage based on the gages individual Location, Material, and SF. Let’s break down exactly how this happens. Microsoft Excel 2007 - Lookup & Logical Functions - 40 - The first three items evaluated by the function are the expressions (B2=$G$2:$G$17), (C2=$H$2:$H$17), and (D2=$I$2:$I$17). (B2=$G$2:$G$17) evaluates to: (C2=$H$2:$H$17) evaluates to: Microsoft Excel 2007 - Lookup & Logical Functions - 41 - (D2=$I$2:$I$17) evaluates to: Next, excel performs the coercion which turns Boolean values (TRUE, FALSE) into integers (1,0) and multiplies all the arrays together. Microsoft Excel 2007 - Lookup & Logical Functions - 42 - Notice that when all three criteria are met the final value is multiplied by three 1’s. If any one of the criteria is FALSE the end product is 0. In this way when the sum of the products is made as the final step, only the value which meets all three criteria is returned: SUMPRODUCT((B2=$G$2:$G$17)* (C2=$H$2:$H$17)* (D2=$I$2:$I$17)* $J$2:$J$17) = 2 This may seem a little complicated but once you become proficient with its use SUMPRODUCT is actually one of the easiest ways to perform a lookup based on multiple criteria. Microsoft Excel 2007 - Lookup & Logical Functions - 43 - IF The IF function is a logic function that has the ability to make a decision and perform an action based on whether a specified condition is TRUE or FALSE. IF has the following parameters. IF Explained: IF(logical_test, value_if_true, value_if_false) logical_test is any value or expression that can be evaluated to TRUE or FALSE. This argument will accept any comparison operator (see table below for a list of comparison operators). value_if_true is an optional parameter that is returned if the logical_test is TRUE. If this parameter is omitted and the logical_test is TRUE the function will return 0. If you want the function to return the word TRUE if the logical_test is TRUE set the parameter to the value TRUE. This parameter can be either static values or another expression. value_if_false is an optional parameter that is returned if the logical_test is FALSE. If this parameter is omitted and the logical_test is FALSE the function will return the value FALSE. This parameter can be either a static value or another expression. Microsoft Excel 2007 - Lookup & Logical Functions - 44 - Example 1 This example will demonstrate the basic functionality of the IF statement on a simple data table. The table below shows a series of stress readings taken from instrumentation on a piece of machinery. These readings are not allowed to exceed a maximum value of 130. Our job is to create an IF statement in column C that will output the word “Warning!” if the stress value exceeds the allowable value. 1. Select cell C2 and enter the following (without the quotes) to begin the function: “=IF(” 2. For the logical_test parameter enter “A2>B2” (without the quotes). 3. For the value_if_true parameter enter “Warning!” (including the quotes). Since we want the function to return a text value we must enclose the value in quotes. 4. For the value_if_false parameter enter two quotes “ ”. This is standard syntax in Excel to denote “nothing”. Microsoft Excel 2007 - Lookup & Logical Functions - 45 - 5. Add the ending parentheses “)” and hit enter. Drag the formula all the way down to the end of the list and wherever the stress value exceeds the allowable value “Warning!” appears. If everything was done correctly the function should look like this: Microsoft Excel 2007 - Lookup & Logical Functions - 46 - Example 2 This example will demonstrate what is typically called a nested IF function. A nested IF function is where a second IF statement is embedded inside the original IF statement. It is not uncommon to have nested IF functions 2 or 3 levels deep. The table below shows the sales of several salesmen over a one week period. The salesmen who sold more than $1,000 get a bonus except for Peter Gibbons. Peter is being disciplined for forgetting to put new cover sheets on his TPS reports so he will not be getting a bonus regardless of whether his sales exceed $1,000. Our job is to enter an IF function into column C that will indicate whether each employee should receive their bonus. 1. Select cell C2 and enter the following (without the quotes) to begin the function: “=IF(” 2. For the logical_test parameter enter “B2>1000” (without the quotes). 3. For the value_if_true parameter enter the following nested IF function: IF(A2=“Peter Gibbons”,“No”,“Yes”) a. In this function the logical_test parameter is “A2=Peter Gibbons”. b. The value_if_true parameter is set to “No” since is does not matter whether Peter met his sales goals. He is not getting a bonus regardless. c. The value_if_false parameter is set to “Yes”. This takes care of all other employees who made their sales goal. The “Yes” indicates they will be getting a bonus. 4. For the value_if_false parameter of the original IF function enter “No”. Nobody will be getting a bonus if they did not meet the sales goal. Microsoft Excel 2007 - Lookup & Logical Functions - 47 - 5. Add the ending parentheses “)” and hit enter. Drag the formula down to the end of the list. If everything was done correctly the formula will look like this: Microsoft Excel 2007 - Lookup & Logical Functions - 48 - AND The AND function is a logic function that returns TRUE if all of its parameters are TRUE. If one or more of its parameters is FALSE the function will return FALSE. Normally the AND function is not used on its own. It is typically used in conjunction with an IF statement. The AND function has the following parameters. AND Explained: AND(logical1, logical2, logical3, …) logical1, logical2, logical3 are 1 to 30 conditions that can evaluate to TRUE or FALSE. These arguments will accept any comparison operator. Example 1 This example builds on the last example by introducing an AND statement into the nested IF function. Another week has passed and the salesmen were given another incentive. This week they will get a bonus if they exceed $1,000 in sales and have at least 2 referrals. Peter is still being disciplined for his TPS reports so he will not receive the bonus regardless of his sales or referrals. 1. The old formula in cell C2 was: =IF(B2>1000,IF(A2=“Peter Gibbons”,“No”,“Yes”),“No”) This function based the bonus solely on Sales. 2. We now want to edit this function to take into account both Sales and Referrals. Microsoft Excel 2007 - Lookup & Logical Functions - 49 - 3. Replace “B2>1000” in the original function with the following: AND(B2>1000,C2>=2) a. This function will return TRUE only if both conditions are TRUE. b. The first logical condition, B2>1000, tests whether sales were over $1,000. c. The second logical condition, C2>=2, tests whether at least 2 referrals were accumulated. 4. Hit enter after the change has been made and drag the formula down to the end of the list. If everything was done correctly the function should look like this: Microsoft Excel 2007 - Lookup & Logical Functions - 50 - OR The OR function is much like the AND function except it returns TRUE if any one of its arguments is TRUE. The OR function has the following arguments. OR Explained: OR(logical1, logical2, logical3, …) logical1, logical2, logical3 are 1 to 30 conditions that can evaluate to TRUE or FALSE. These arguments will accept any comparison operator. Example 1 This example will build on the last example by adding an OR statement inside the nested IF statement. Since very few people achieved their bonuses last week the incentive is the same this week. Everyone will receive a bonus if they make sales of over $1,000 and have at least 2 referrals. However, Bob Porter also recently forgot his TPS report cover sheets so he, along with Peter Gibbons, will not be receiving his bonus regardless of his sales or referrals. 1. Our last equation in Cell D2 with the imbedded AND function was the following: =IF(AND(B2>1000,C2>=2),IF(A2=“Peter Gibbons”,“No”,“Yes”),“No”) This function was based on sales and referrals but only excluded Peter Gibbons from the bonus. Microsoft Excel 2007 - Lookup & Logical Functions - 51 - 2. We want to edit this function so that both Peter Gibbons and Bob Porter are excluded from bonuses. This can be accomplished by replacing the current logical_test in the nested IF with an OR statement. Replace: A2=“Peter Gibbons” with the following OR statement: OR(A2=“Peter Gibbons”,A2=“Bob Porter”) a. The first logical condition in this OR statement A2=“Peter Gibbons” tests for Peter Gibbons as the salesman. b. The second logical condition in the OR statement A2=“Bob Porter” tests for Bob Port as the salesman. c. If either of these conditions is TRUE the OR function will return TRUE and the nested IF function will return “No” since both of these men are excluded from bonuses. 3. Hit enter after making the change and drag the formula down to the end of the list. If everything was done correctly the function should look like this: Microsoft Excel 2007 - Lookup & Logical Functions - 52 -