MODULE VI: MORE FUNCTIONS Copyright 2012, National Seminars Training
More Functions Using the VLOOKUP and HLOOKUP Functions Lookup functions look up values in a table and return a result based on those values. Before you can use a lookup function, you must first create a table in the worksheet. This table consists of a column or row of values called the lookup values, which is used to compare with the value entered in the cell. The rest of the table contains the corresponding data for each lookup value. There are two lookup functions: Vertical and Horizontal. The name is based on the shape of the table. Vertical is for a table in columns, and Horizontal is for a table in rows. The VLOOKUP function expects the first value to be in the first column. The HLOOKUP expects the first value to be in the first row. The second value you look up determines the number of columns or rows counted over from the first column or row. Both the VLOOKUP and HLOOKUP functions have three arguments that appear in the following order: Lookup value, Table array, and Column (or Row) index number. The lookup value is the value you are looking up in the first column of the table. It can be a value, a text string, or a cell reference. The table array is the range address of the table of information. The column (or row) index number is the number of columns (or rows) Excel needs to count over to the right (or down) from the first column (or row) in a table. The VLOOKUP function searches for the lookup value in the leftmost column of the table array. Once the lookup value has been located, Excel counts to the right the number of columns specified in the column index number to find the result of the function. The lookup table must be sorted by the items in the first column of the table, ascending in either alphabetical (strings) or numerical (values) order. 2
Steps: 1. Select the cell where you want the result of the VLOOKUP function. 2. On the Formulas tab in the Function Library group, select the Insert Function button (or click the Insert Function button on the formula bar or the Paste Function button on the standard toolbar). 3. In the Select a function box, select VLOOKUP. 4. In the Lookup_value box, type or select the lookup value. 5. In the Table_array box, type or select the range that contains the table (or click the [F3] key and select the range name if appropriate). 6. In the Col_index_num box, type the appropriate column number (this is a count including the first column). 7. In the Range_lookup box, type FALSE for an exact match or leave blank for the closest match. 8. Click the OK button. 3
4
Using the IF Function Logical functions make decisions based on criteria. If the criteria evaluation is true, one action is taken; if the criteria evaluation is false, a different action is taken. This decision-making capability of logical functions can be applied to many different situations. The IF function returns one value if a condition is true and another value if a condition is false. You can also use the IF function to display text as a result of a logical test, but you must enclose the text in the formula in quotation marks. The syntax of an IF function is: =IF(logical test,value if true,value if false) The components of the formula are described in the following table. Component Logical test Value if true Value if false Description The test condition. It can contain cell references, text in quotes, cell names, and numbers. The items are compared using the following comparison operators: = equal to; <> not equal to; > greater than; >= greater than or equal to; < less than; <= less than or equal to. The result produced if the logical test is true. It can be a number, formula, cell reference, cell name, text in quotes, or another function. The result produced if the logical test is false. It can be a number, formula, cell reference, cell name, text in quotes, or another function. 5
Steps: 1. Select the cell where you want the result of the IF function to appear. 2. On the Formulas tab in the Function Library group, select the Insert Function button (or click the Insert Function button on the formula bar or the Paste Function button on the standard toolbar). 3. In the Select a function box, select IF. 4. In the Logical_test box, type or create the logical test. 6
5. In the Value_if_true box, type or create the formula. 6. In the Value_if_false box, type the appropriate formula or result. 7. Click the OK button. Note: You can use an IF function within another IF function to create a nested IF function. A nested IF function allows you to test for a second condition if the first condition is found false. The syntax of a nested IF function is: =IF(logical test,value if true,if(logical test,value if true,value if false)) You can create up to seven nested IF functions within an IF function. 7
Macros A macro is a small program you can use to automatically perform frequently used operations. It contains all the commands, mouse movements, and other actions necessary to complete a task. Excel macros are written in the Visual Basic for Applications (VBA) programming language. The series of commands used in a macro are known as a procedure. Each procedure is given a name that is used to execute the macro. Each procedure is saved in a device called a module. This module is attached to the worksheet in which the macro was stored. A module cannot be seen in the normal window view because it is hidden. You must use the Visual Basic Editor window to select a module and view and edit the macro commands. When you record a procedure, Visual Basic translates the menu choices and keystrokes into the macro statements. A macro can be stored in a specific workbook or in the Personal Macro Workbook. Macros stored in a specific workbook can only be used when that workbook is open. The Personal Macro Workbook automatically opens when you start Excel; therefore, macros stored in this workbook are available to all files. Although Excel 2010 and 2007 have a new interface, recording a macro and using the Visual Basic Editor is virtually unchanged from previous versions of Excel. You can create a macro using the macro recorder. You perform the steps you want to include in the macro, and Excel records them into a macro procedure. Displaying the Developer Tab The Developer tab is not displayed by default. If the Developer tab is not yet visible in Excel, you need to do the following to display it (this is not necessary in 2003). Version 2010 Steps: 1. Click the File tab, then click Options, then click Customize Ribbon. 2. Under Customize the Ribbon and under Main Tabs, select the Developer check box. 3. Click the OK button. Version 2007 Steps: 1. Click the Office Button, and then click the Excel Options button on the bottom of the dialog box. 2. In the Popular category, under Top options for working with Excel, select the Show Developer tab in the Ribbon check box. 3. Click the OK button. 8
To Record a Macro Steps: 1. On the Developer tab in the Code group, click Record Macro (or display the Visual Basic toolbar and click the Record Macro button). 2. Type a name for the macro. (The first character of the macro name must be a letter. The following characters can be letters, numbers, or underscored characters, but spaces are not allowed in a macro name.) 3. To create a shortcut key, select the Shortcut key text box. (Be careful: The shortcut key will override any equivalent default Excel shortcut key while the workbook that contains the macro is open.) 4. Type the character(s) you want to use as the shortcut key, if any. 5. Select the Store macro in list, then select the desired location. 6. Click in the Description text box and type a macro description if desired. 7. Click the OK button. 8. Continue recording each step of the macro. 9. On the Developer tab in the Code group, click Stop Recording (or click the Stop Recording button on the Stop Recording toolbar when finished). Tip: If you want a macro to be available whenever you use Excel, select the Personal Macro Workbook. Excel will then create a hidden personal macro workbook (Personal.xlsb) (or personal.xls) if it does not already exist and save the macro in that workbook. 9
Running a Macro Macros can be run by using the shortcut key(s) if you assigned one, or you can use the following steps. Steps: 1. Open a workbook containing macros and enable the macros, if necessary. 2. On the Developer tab in the Code group, click the Macros button (or select the Tools menu, point to the Macros command, and then select the Macros command). 3. In the Macro name: box, click the macro that you want to run. 4. Click the Run button. 10
Creating a PivotTable Report A PivotTable report summarizes large amounts of data quickly. It allows you to manipulate row and column headings around the central data so that you can view the data in different ways. PivotTable reports are organized into fields and items. Fields are rows or columns of data. Subcategories are items within a field. For example, if you want to summarize data by month, each month becomes a line item in the field. Data fields contain the numbers you want to summarize or analyze, such as sales or volume. You can filter the items using a page field. A page field displays only the data for the selected page field item. Excel has a PivotTable and PivotChart Wizard to take you through the steps of creating a PivotTable report. First, you select the source of the original data, such as a list or database on an Excel worksheet or an external database. This date must have column headings. Next, you select the range of data you want to use. The final step of the PivotTable and PivotChart Wizard is to select the worksheet and cell locations for the table. Steps: 1. Select any cell in the database list. 2. On the Insert tab in the Tables group, click the bottom half of the PivotTable button and select the PivotTable command (or select the Data menu, select the PivotTable and PivotChart Report command, select the source of the data and the kind of report you want to create, then click the Next > button twice). 3. Select where you want to place the PivotTable report. 4. Click the OK button (or click Finish). 11
Adding PivotTable Report Fields The four sections of the PivotTable report are Report Filter (Page Fields), Row Labels (Row Fields), Column Labels (Column Fields), and Values (Data Items). The Values (Data Items) area contains the fields that are calculated. Therefore, they are usually, but not limited to, numerical fields. In versions 2010 and 2007, to set up the report you can drag the field names on the PivotTable Field List to one of the four sections. (In version 2003, use the PivotTable toolbar and the Show Fields List button if necessary to display the field list, then simply drag the field names directly into the four sections of the pivot table template.) 12