Chapter 12. Building Formulas

Formula Fundamentals

Using Functions: A Preview

Working with Formulas

Worksheet Calculation

Using Arrays

Linking Workbooks

Creating Conditional Tests

FORMULAS are the heart and soul of a spreadsheet, and Microsoft Excel 2010 offers a rich environment in which to build complex formulas. Armed with a few mathematical operators and rules for cell entry, you can turn a worksheet into a powerful calculator. In this chapter, we cover the basics and then look more closely at using functions, defining names, building structured references, working with arrays, creating linking formulas, and constructing conditional tests.

A lot of this section will seem elementary to experienced Excel users, but it is important information for anyone who is just arriving at the party. And even experienced users might find something useful here that they didn’t know about.

All formulas in Excel begin with an equal sign. This is the most fundamental fact of all. The equal sign tells Excel that the succeeding characters constitute a formula. If you omit the equal sign, Excel might interpret the entry as text. To show how formulas work, we’ll walk you through some rudimentary ones. Begin by selecting blank cell A10. Then type =10+5, and press Enter. The value 15 appears in cell A10. Now select cell A10, and the formula bar displays the formula you just typed. What appears in the cell is the displayed value; what appears in the formula bar is the underlying value, which in this case is a formula.

Operators are symbols that represent specific mathematical operations, including the plus sign (+), minus sign (–), division sign (/), and multiplication sign (*). When performing these operations in a formula, Excel follows certain rules of precedence:

Type some formulas to see how these rules apply. Select an empty cell, and type =4+12/6. Press Enter, and you see the value 6. Excel first divides 12 by 6 and then adds the result (2) to 4. Then select another empty cell, and type =(4+12)/6. Press Enter, and you see the value 2.666667. This demonstrates how you can change the order of precedence by using parentheses. The formulas in Table 12-1 contain the same values and operators, but note the different results caused by the placement of parentheses.

If you do not include a closing parenthesis for each opening parenthesis in a formula, Excel displays the message “Microsoft Excel found an error in this formula” and provides a suggested solution. If the suggestion matches what you had in mind, simply press Enter, and Excel completes the formula for you.

When you type a closing parenthesis, Excel briefly displays the pair of parentheses in bold. This feature is handy when you are typing a long formula and are not sure which pairs of parentheses go together.

A cell reference identifies a cell or group of cells in a workbook. When you include cell references in a formula, the formula is said to be linked to the referenced cells. The resulting value of the formula depends on the values in the referenced cells and changes automatically when the values in the referenced cells change.

To see cell referencing at work, select cell A1, and type the formula =10*2. Now select cell A2, and type the formula =A1. The value in both cells is 20. If at any time you change the value in cell A1, the value in cell A2 changes also. Now select cell A3, and type =A1+A2. Excel returns the value 40. Cell references are especially helpful when you create complex formulas.

Relative references—the type we’ve used so far in the sample formulas—refer to cells by their position in relation to the cell that contains the formula, such as “the cell two rows above this cell.” A relative reference to cell A1, for example, looks like this: =A1.

Absolute references refer to cells by their fixed position in the worksheet, such as “the cell located at the intersection of column A and row 2.” An absolute reference to cell A1 looks like this: =$A$1.

Mixed references contain a relative reference and an absolute reference, such as “the cell located in column A and two rows above this cell.” A mixed reference to cell A1 looks like this: =$A1 or =A$1.

Dollar signs in a cell reference indicate its “absoluteness.” If the dollar sign precedes only the letter (A, for example), the column coordinate is absolute, and the row is relative. If the dollar sign precedes only the number (1, for example), the column coordinate is relative, and the row is absolute.

Absolute and mixed references are important when you begin copying formulas from one location to another in your worksheet. When you copy and paste, relative references adjust automatically, but absolute references do not. For information about copying cell references, see How Copying Affects Cell References on page 472.

While you are entering or editing a formula, press F4 to change reference types quickly. The following steps show how:

When you use this technique to change reference types, click the formula bar to activate it, and then, before pressing F4, click in the cell reference you want to change or drag to select one or more cell references in the formula to change all the selected references at the same time.

You can refer to cells in worksheets in separate workbooks in the same way you refer to cells in other worksheets within the same workbook. These references are called external references. For example, to enter a reference to Book2 in Book1, follow these steps:

One of the handiest benefits of using references is the ability to copy and paste formulas. But you need to understand what happens to your references after you paste so that you can create formulas with references that operate the way you want them to operate.

Copying Relative References When you copy a cell containing a formula with relative cell references, Excel changes the references automatically relative to the position of the cell where you paste the formula. Referring to Figure 12-2, suppose you type the formula =AVERAGE(B4:E4) in cell F4. This formula averages the values in columns B through E.

You want to include this calculation for the remaining rows as well. Instead of typing a new formula in each cell in column F, select cell F4 and press Ctrl+C to copy it (or click the Copy button in the Clipboard group on the Home tab). Then select cells F5:F8, click the arrow next to the Paste button on the Home tab, click Paste Special, and then select the Formulas And Number Formats option (to preserve the cell and border formatting). Figure 12-3 shows the results. Because the formula in cell F4 contains a relative reference, Excel adjusts the references in each copy of the formula. As a result, each copy of the formula calculates the average of the cells in the corresponding row. For example, cell F5 contains the formula =AVERAGE(B5:E5).

Copying Absolute References If you want cell references to remain the same when you copy them, use absolute references. For example, in the worksheet on the left in Figure 12-4, cell B2 contains the hourly rate at which employees are to be paid, and cell C5 contains the relative reference formula =B2*B5. Suppose you copy the formula in C5 to the range C6:C8. The worksheet on the right in Figure 12-4 shows what happens: You get erroneous results. The formulas in cells C6:C8 should refer to cell B2, but they don’t. For example, cell C8 contains the incorrect formula =B5*B8.

Because the reference to cell B2 in the original formula is relative, it changes as you copy the formula to the other cells. To correctly apply the wage rate in cell B2 to all the calculations, you must change the reference to cell B2 to an absolute reference before you copy the formula.

To change the reference style, click the formula bar, click the reference to cell B2, and then press F4. The result is the following formula: =$B$2*B5.

When you copy this modified formula to cells C6:C8, Excel adjusts the second cell reference within each formula but not the first. In Figure 12-5, cell C8 now contains the correct formula: =$B$2*B8.

Copying Mixed References You can use mixed references in your formulas to anchor a portion of a cell reference. (In a mixed reference, one portion is absolute, and the other is relative.) When you copy a mixed reference, Excel anchors the absolute portion and adjusts the relative portion to reflect the location of the cell to which you copy the formula.

To create a mixed reference, you can press the F4 key to cycle through the four combinations of absolute and relative references—for example, from B2 to $B$2 to B$2 to $B2.

The loan payment table in Figure 12-6 uses mixed references (and an absolute reference). You need to enter only one formula in cell C6 and then copy it down and across to fill the table. Cell C6 contains the formula = –PMT($B6,$C$3,C$5) to calculate annual payments on a loan. We copied this formula to all the cells in the range C6:F10 to calculate payments using three additional loan amounts and four additional interest rates.

The first cell reference, $B6, indicates that we always want to refer to the values in column B but the row reference (Rate) can change. Similarly, the mixed reference, C$5, indicates we always want to refer to the values in row 5 but the column reference (Loan Amount) can change. For example, cell E8 contains the formula = –PMT($B8,$C$3,E$5). Without mixed references, we would have to edit the formulas manually in each of the cells in the range C6:F10.

You edit formulas the same way you edit text entries: click in the cell or formula bar, click or drag to select characters, press Backspace or Delete or start typing. To replace a cell reference, highlight it and click the new cell you want the formula to use; Excel enters a relative reference automatically. You can also just click to place the insertion point where in the formula you want to insert a reference. To include cell B1 in the formula =A1+A3, place the insertion point between A1 and the plus sign, type another plus sign, and then click cell B1. Excel inserts the reference and the formula becomes =A1+B1+A3.

The term text value refers to any entry that is neither a number nor a numeric text value (see the previous section); Excel treats the entry as text only. You can refer to and manipulate text values by using formulas. For example, if cell A1 contains the text First and you type the formula =A1 in cell A10, cell A10 displays First.

You can use the & (ampersand) operator to concatenate, or join, several text values. Extending the preceding example, if cell A2 contains the text Quarter and you type the formula =A1&A2 in cell A3, then cell A3 displays FirstQuarter. To include a space between the two strings, change the formula to =A1&” “&A2. This formula uses two concatenation operators and a literal string, or string constant (in this case, a space enclosed in quotation marks).

You can use the & operator to concatenate strings of numeric values as well. For example, if cell A3 contains the numeric value 867 and cell A4 contains the numeric value 5309, the formula =A3&A4 produces the string 8675309. This string is left-aligned in the cell because it’s considered a text value. (Remember, you can use numeric text values to perform any mathematical operation as long as the numeric string contains only the numeric characters listed in the previous section.)

Finally, you can use the & operator to concatenate a text value and a numeric value. For example, if cell A1 contains the text January and cell A3 contains the numeric value 2009, the formula =A1&A3 produces the string January2009.