Section 8.3. Copying Formulas


8.3. Copying Formulas

Sometimes you need to perform similar calculations in different cells throughout a worksheet. For example, say you want to calculate sales tax on each item in a product catalog, the monthly sales in each store of a company, or the final grade for each student in a class. In this section, you'll learn how Excel makes it easy with relative cell references . Relative cell references are cell references that Excel updates automatically when you copy them from one cell into another. They're the standard kind of references that Excel uses (as opposed to absolute cell references, which are covered in the next section). In fact, all the references you've used so far have been relative references, but you haven't yet seen how they work with copy-and-paste operations.

Consider the worksheet shown in Figure 8-11, which contains a teacher's grade book. In this example, each student has three grades: two tests and one assignment. A student's final grade is based on the following percentages: 25 percent for each of the two tests, and 50 percent for the assignment.

Figure 8-11. This worksheet shows a list of students in a class, and calculates the final grade for each student using two test scores and an assignment score. So far, the only formula that's been added is for the first student (in cell E2).


The following formula calculates the final grade for the first student (Edith Abbott):

 =B2*25% + C2*25% + D2*50% 

The formula that calculates the final mark for the second student (Grace DeWitt) is almost identical. The only change is that all the cell references are offset by one row, so that B2 becomes B3, C2 becomes C3, and D2 becomes D3:

 =B3*25% + C3*25% + D3*50% 

You may get fed-up entering all these formulas by hand. A far easier approach is to copy the formula from one cell to another. Here's how:

  1. Move to the cell containing the formula you want to copy .

    In this example, you'd move to cell E2.

  2. Copy the formula to the clipboard by pressing Ctrl+C .

    You can also copy the formula by choosing Home Clipboard Copy.

  3. Paste in the new formulas by pressing Ctrl+V .

    You can also paste the formula by choosing Home Clipboard Paste.

    When you paste a formula, Excel magically copies an appropriate version of the formula into each of the cells from E3 to E10. These automatic formula adjustments occur for any formula, whether it uses functions or just simple operators. Excel then automatically calculates and displays the results, as shown in Figure 8-12.

Figure 8-12. When you paste the formula into one or more new cells, each Final Grade formula operates on the data in its own row. This means that you don't have to tweak the formula for each student. The formula bar shows the formula contained in cell E3.



Tip: There's an even quicker way to copy a formula to multiple cells by using the AutoFill feature introduced in Chapter 2 (Section 2.2.3). In the student grade example, you'd start by moving to cell E2, which contains the original formula. Then, you'd click the small square at the bottom-right corner of the cell outline, and drag the outline down until it covers all cells from E3 to E10. When you release the mouse button, Excel inserts the formula copies in the AutoFill region.

8.3.1. Absolute Cell References

Relative references are a true convenience since they let you create formula copies that don't need the slightest bit of editing. But you've probably already realized that relative references don't always work. For example, what if you have a value in a specific cell that you want to use in multiple calculations? You may have a currency conversion ratio that you want to use in a list of expenses. Each item in the list needs to use the same cell to perform the conversion correctly. But if you make copies of the formula using relative cell references, then you'll find that Excel adjusts this reference automatically and the formula ends up referring to the wrong cell (and therefore the wrong conversion value).

POWER USERS' CLINIC
Cell Formulas "Under the Hood"

To understand how Excel adjusts copied formulas, you need to know a little more about how Excel stores formulas. Internally, Excel formulas are actually stored using a system called R1C1 reference (Section 1.3). With R1C1 referencing, when you create a formula, it doesn't contain cell references; instead, it contains cell offsets . Offsets tell Excel how to find a cell based on its position relative to the current cell, as you'll see below.

For example, in the student grade calculation, the formula:

 =B2*25% + C2*25% + D2*50% 

looks like this in R1C1 representation:

 =RC[-3]*25% + RC[-2]*25% + RC[-1]*50% 

Notice that Excel has translated the cell reference B2 into the notation RC[-3], which means "get the number in the same row, but three columns to the left." If the formula were using a number from the row above, then you'd see an R1C1 reference like R[-1]C[-3], which would tell Excel to go one row up and three columns to the left. Negative numbers in relative cell referencing indicate movement to the left (for columns ) or up (for rows); positive numbers indicate movement to the right (columns) or down (rows).

When you copy a formula from one cell to another, Excel actually copies the R1C1 formula, rather than the formula you've entered (unless you've instructed Excel to use absolute cell references, as explained in the next section). To view your formulas in R1C1 style, you can temporarily change the type of cell addressing used in your spreadsheet. (See Section 1.3 for instructions.) You'll probably want to turn this mode off when you're about to create a formula, as it's almost always easier to write formulas using Excel's standard cell addressing.


Figure 8-13 illustrates the problem with the worksheet of student grades. In this example, the test and assignment scores aren't all graded out of 100 possible points; each item has a different total score available (listed in row 12). In order to calculate the percentage a student earned on a test, you need to divide the test score by the total score available. This formula, for example, calculates the percentage for Edith Abbott's performance on Test B:

 =B2/B12*100% 

To calculate Edith's final grade for the class, you'd use the following formula:

 =B2/B12*25% + C2/C12*25% + D2/D12*50% 

Like many formulas, this one contains a mix of cells that should be relative (the individual scores in cells B2, C2, and D2) and those that should be absolute (the possible totals in cell B12, C12, and D12). As you copy this formula to subsequent rows, Excel incorrectly changes all the cell references, causing a calculation error.

Fortunately, Excel provides a perfect solution. It lets you use absolute cell references cell references that always refer to the same cell. When you create a copy of a formula that contains an absolute cell reference, Excel doesn't change the reference (as it does when you use relative cell references; see the previous section). To indicate that a cell reference is absolute, use the dollar sign ($) character. For example, to change B12 into an absolute reference, you would add the $ character twice, once in front of the column and once in front of the row, which changes it to $B$12.

Figure 8-13. In this version of the student grade book, both the tests and the assignment are graded on different scales (as listed in row 12). Thus, the formula for calculating the final class grade uses the values in cells B12, C12, and D12. When you copy the Final Grade formula from the first row (cell E2) to the rows below it, Excel offsets the formula to use B13, C13, and D13none of which exist. Thus a problem occursshown here as a divide-by-zero error. To fix this, you need to use absolute cell references.


Here's the corrected class grade formula (for Edith) using absolute cell references:

 =B2/$B*25% + C2/$C*25% + D2/$D*50% 

This formula still produces the same result for the first student. However, you can now copy it correctly for use with the other students. To copy this formula into all the cells in column E, use the same procedure described in the previous section on relative cell references.

8.3.2. Partially Fixed References

You might wonder why you need to use the $ character twice in an absolute reference (before the column letter and the row number). The reason is that Excel lets you create partially fixed references. To understand partially fixed references, it helps to remember that every cell reference consists of a column letter and a row number. With a partial fixed reference, Excel updates one component (say, the column part) but not the other (the row) when you copy the formula. If this sounds complex (or a little bizarre), consider a few examples:

  • You have a loan rate in cell A1, and you want all loans on an entire worksheet to use that rate in calculations. If you refer to the cell as $A$1, its column and row always stay the same when you copy the formula to another cell.

  • You have several rows of loan information. The first column of a row always contains the loan rate for all loans on that row. In your formula cell, if you refer to cell $A1, then when you copy the formula across columns and rows, the row changes (2, 3, 4, etc.) but the column doesn't (A2, A3, A4, etc.).

    UP TO SPEED
    Creating an Exact Formula Copy

    There's another way to copy a formula that prevents Excel from automatically changing the formula's cell references. The trick's to copy the formula itself rather than copy the whole cell (which is what you do when performing a basic copy-and-paste operation on a formula).

    The process takes a few more steps, and it lets you paste only one copy at a time, but it can still come in handy if you don't want Excel to use relative references. Here's how it works:

    1. First, move to the cell that contains the formula you want to copy.

    2. Place this cell in edit mode by double-clicking it or pressing F2.

    3. Select all the text in the cell. You can use the mouse, or you can use the arrow keys (just hold down Shift as you scroll from the beginning to the end of the cell).

    4. Once you've selected the complete formula, press Ctrl+C to copy it.

    5. Press Enter to leave edit mode once you're finished.

    6. Move to the new cell, and press Ctrl+V to paste it.

    Keep in mind that when you use this approach, you create an exact copy of the formula. That means this technique doesn't help in situations where some cell references need to be absolute, and others need to be relative.


  • You have a table of loan rates organized by the length of the loan (10-year, 15-year, 20-year, etc.) along the top of a worksheet. Loans in each column are calculated using the rate specified at the top of that column. If you refer to the rate cell as A$1 in your first column's formula, then the row stays constant (1), but the column changes (B1, C1, D1, etc.) as you copy the formula across columns and down rows.


Tip: You can quickly change formula references into absolute or partially fixed references. Just put the cell into edit mode (by double-clicking it or pressing F2). Then, move through the formula until you've highlighted the appropriate cell reference. Now, press F4 to change the cell reference. Each time you press F4, the reference changes. If the reference is A1, for instance, it becomes $A$1, then A$1, then $A1, and then A1 again.

8.3.3. Referring to Other Worksheets and Workbooks

Most formulas refer to the cells in a single worksheet. Excel does, however, let you use formulas that refer to cells in other worksheets or even other files.

To reference a cell in another worksheet, you simply need to preface the cell with the worksheet name , followed by an exclamation mark. For example, say you've created a formula to double the value of the number in cell A1 on a worksheet named Sheet1. You'd use this formula:

 =A1*2 

If you want to use the same formula in another worksheet (in the same workbook), you'd insert this formula in the new worksheet:

 =Sheet1!A1*2 


Note: If you use the point-and-click method to build formulas, you'll find that you don't need to worry about the syntax for referring to cells on other worksheets. If you switch to another worksheet while you're building a formula, Excel automatically inserts the correct reference to the worksheet name.

It's fairly common for one worksheet to reference another within the same workbook file. (For a refresher on the difference between worksheets and workbooks see Section 4.1.) in Section 9.1.1, you'll see an example where one worksheet includes a product catalog, and a second worksheet builds an invoice. In that example, the second worksheet uses formulas that refer to data on the first sheet.

It's less common for a worksheet to refer to data in another file . The potential problem with this type of link is that there's no way to guarantee that referenced files will always be available. If you rename the file or move it to a different folder, then the link breaks. Fortunately, Excel doesn't leave you completely stranded in this situationinstead, it continues with the most recent version of the data it was able to retrieve. You can also tweak the link to point to the new location of the file.

To create a link to a cell in another workbook, you need to put the file name at the beginning of the reference and then enclose it in square brackets. You follow the file name with the sheet name, an exclamation mark, and the cell address. Here's an example:

 =[SimpleExpenses.xlsx]Sheet1!B3 

If the file name or the sheet name contains any spaces, you need to enclose the whole initial portion inside apostrophes , like so:

 ='[Simple Expenses.xlsx]Sheet1'!B3 

When you enter this formula, Excel checks to see if this file is already open in another Excel window. If not, then Excel attempts to find the file in the current folder (that is, the same folder that holds the workbook you're editing). If it can't find the file, it shows a standard Open dialog box. You can use this dialog box to browse to the file you want to use. Once you select the file, Excel updates the formula accordingly .

Excel performs a little sleight of hand with formulas that reference other files. If the referenced workbook's currently open, the formula displays only the file name. However, once you close the referenced file, the formula changes so that it includes the full folder path . Excel makes this change automatically, whether you type a file name in by hand or build the formula by pointing and clicking on cells in another open workbook.

Here's an example of what the full formula looks like when you close the file that contains the Sheet1 worksheet (split over two lines to fit the page):

 ='[C:\Documents and Settings\Matthew\My Documents\Simple Expenses.xlsx]Sheet1'!B3 

8.3.3.1. Updating formulas that refer to other workbooks

You use a formula that refers to another workbook (rather than just copying the information you need) when you know the information in that workbook might change. So what does Excel do to keep your linked information up to date? It all depends.

If the linked workbook is currently open, Excel spots any changes right away. For example, imagine you have a workbook named Summary.xlsx that refers to cell B3 in a workbook named SimpleExpenses.xlsx (as shown in the previous example). When you modify the number in B3 in SimpleExpenses.xlsx, Excel recalculates the linked formula in Summary.xlsx immediately.

It's a different story if you work with both files separately . For example, imagine you close Summary.xlsx, change the total in SimpleExpenses.xlsx, and then close SimpleExpenses.xlsx. The next time you open Summary.xlsx, Excel is in a difficult position. It needs to get the updated information in SimpleExpenses.xlsx, but it doesn't want to open the file to do so (at least not without your OK). So instead, the Summary.xlsx file uses the most recent informationthe value it pulled out of cell B3 the last time SimpleExpenses.xlsx was open. Excel also shows a message bar just above the grid of cells, warning you that it hasn't retrieved the latest information (Figure 8-14).

Figure 8-14. The security warning tells you that the workbook you've opened has linked cells, but Excel hasn't tried to update them. That's because updating them involves opening another file, which ultraparanoid Excel isn't ready to do.


If you want to update your linked formulas, click the message bar's Options button (shown in Figure 8-14). When the Security Options dialog box appears, choose "Enable this content", and then click OK. Excel opens the linked workbook (in this case, SimpleExpenses.xlsx) behind the scenes, and then uses the latest information to recalculate your formula.


Tip: You need to click the Options button, and then turn on your links every time you open a workbook like Summary.xlsx. If you get tired of this process, then you may be interested in creating a trusted location a folder on your hard drive where you can store your workbooks so Excel knows they're kosher. When you use a trusted location, Excel doesn't give you a security warninginstead, it refreshes your linked formulas automatically. Section 27.3.2 shows how to set up a trusted location.

Sometimes, despite the best of intentions, your formula points to a workbook that Excel can't find. For example, if you rename SimpleExpenses.xlsx or move it to another folder, then you'll run into this problem. In this situation, Excel gives you an error message when you attempt to switch on your links (Figure 8-15).

Figure 8-15. If Excel can't find the workbook you referenced, you see an error message that gives you the choice of ignoring it and working with what you have (click Continue), or relinking the workbook to the correct file (click Edit Links and see Figure 8-16).


If you know that a link's gone bad, you've got a problemdepending on how many linked formulas your workbook has, you could be stuck updating dozens of cells. Fortunately, the Edit Links dialog box (shown in Figure 8-16) gives you an easier alternative. Rather than changing each formula by hand, you can "relink" everything by pointing Excel to the right file. The Edit Links dialog box appears automatically when an error occurs, but you don't need to wait for a problem to change a link. You can also get to the Edit Links dialog box on your own by choosing Data Connections Edit Links.

Figure 8-16. In this example, Excel can't find the linked file SimpleExpenses.xlsx. You can select the link, and then click Change Source to browse for the linked workbook (click OK when you find it); or click Break Link to make your workbook independent, and just use whatever data you extracted last time permanently.


The Edit Links dialog box is invaluable for fixing broken links. In addition, some of the buttons in the Edit Links dialog box work with links that aren't broken. They include:

  • Update Values refreshes the formulas that use linked cells. If the linked file has been changed since you opened the current workbook, then this action gets the new values.

  • Open Source opens the linked workbook in another Excel window, so you can really see what's going on.

  • Check Status rechecks the file, and updates the Status column to let you know whether it exists, whether it's currently open, and so on.

  • Startup Prompt opens a Startup Prompt dialog box where you can choose the behavior Excel uses when it opens this workbook. You have three choices: refresh the linked cells automatically, never refresh, or ask you (the standard setting).

FREQUENTLY ASKED QUESTION
How Changing the Location of Cells Affects Formulas

OK, I know how Excel adjusts a formula when I copy it to another location. But what happens if I move cells around after I've created a formula ?

No worries. It turns out that Excel is surprisingly intelligent . Consider the following simple formula:

 =B1+A2 

If you cut and paste the contents of A2 to A3, Excel automatically updates your formula to point to the new cell, without complaining once. It also performs the same automatic cleanup if you drag the contents of a cell to another location (although if you simply make a duplicate copy of the cell, Excel won't change your formula). Excel is also on the ball when you insert and delete rows and columns.

If at any time Excel can't find your cells, the formula changes to show the error code #REF! You can then take a closer look at the formula to find out what really went wrong. For example, if you delete column B from your spreadsheet (by selecting the column and using the Home Cells Delete command), the formula changes to this:

 =#REF!+A2 

Even though there's still a B1 cell in your worksheet (it's the cell that was formerly named C1), Excel modifies the formula to make it clear that you've lost your original data.




Excel 2007[c] The Missing Manual
Excel 2007[c] The Missing Manual
ISBN: 596527594
EAN: N/A
Year: 2007
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net