0593 Formulas Functions in Microsoft Excel (2)
0593 Formulas Functions in Microsoft Excel (2)
Theresa A Scott, MS
Biostatistician III
Department of Biostatistics
Vanderbilt University
[email protected]
Table of Contents
1 Introduction 1
1.1 Using Excel for Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
i
Preface
All of the lecture notes and supplementary sample data files are located at http://biostat.mc.vanderbilt.
edu/TheresaScott under Current Teaching Material.
If you have any questions, feel free to contact me at [email protected] or (615) 343-1713, or drop
by my office D-2217 MCN.
1
Section 1
Introduction
The distinguishing feature of a spreadsheet program such as Excel is that it allows you to create mathematical
formulas and execute functions. Otherwise, it is not much more than a large table displaying text.
❼ Recall, your spreadsheet environment will become a dynamic and responsive work environment when
you use formulas and functions; they automatically updating when you change your data.
❼ In formulas and functions, missing values (i.e. blank cells) are sometimes taken as zeros, when they
should represent data that are truly missing.
❼ Recommend consulting with an experienced statistician when wanting to conduct advanced and/or
statistical analysis, or use packages like SPSS, STATA, R, or SAS.
❼ Tip: Be aware of Excel’s faults and, if possible, set up a simple example to test the function’s handling
of blank cells.
1
Section 2
As mentioned, the ability to perform calculations is one of the purposes of using a spreadsheet application.
Some examples of the types of calculations that can be done are:
❼ totals
❼ subtotals
❼ average
❼ standard deviation
In either case, all formulas and functions are entered in a cell and must begin with an equal sign ’=’.
2
SECTION 2. FORMULAS AND FUNCTIONS 3
❼ The most logical solution would be to type the formula ’=B2+B3+B4+B5’ into cell B6.
You can also enter formulas by using the point mode, where you either click on a cell with your left mouse
button or you use the arrow keys.
❼ To enter ’=B2+B3+B4+B5’ into cell B6 using the point:
– Left click on cell B6 to make it active.
– Type ’=’.
– Use the up arrow key to move to cell B5, or left click on cell B5.
– Type ’+’.
– Use the up arrow or mouse to add cells B4, B3, and B2 in the same fashion.
– Press Enter when you are finished entering the formula.
You’ll notice that the calculation executes immediately after the formula is typed into the cell and entered
by pressing either the Enter or Tab key or by clicking the check mark in the formula bar.
❼ The result of the calculation is displayed in the cell (i.e., in B6).
❼ The formula itself is now visible in the formula bar.
What happens if you enter ’B2+B3+B4+B5’ without the leading equal sign ’=’ into cell B6?
❼ REMEMBER: All formulas and functions must begin with a ’=’.
❼ The text of the formula or function will be displayed in the cell if you do not use an ’=’ and the
calculation will not be executed.
IMPORTANT: Why use cell references (i.e., cell addresses; e.g., B2 or C5) in formulas instead of the
actual values of the cells (e.g., 10000 or 700)? The answer: automatic calculation. Let’s illustrate the con-
cept of automatic calculation with a practice exercise:
→ Practice Exercise: Let’s make a change to the Costs for the Fall and Spring semester spreadsheet and note
how Excel automatically recalculates the formulas and functions we have already entered.
❼ Change the amount entered in cell C2 from ’10000’ to ’15000’.
❼ Notice how all the calculations referencing cell C2 automatically changed.
To reiterate the use of cell addresses in formulas and functions, imagine we had constructed our formu-
las and functions by typing the actual numbers contained in the cells instead of the cell addresses. That
is, to calculate the Total Cost for the Fall semester we had entered the formula ’=10000+5000+1000+1500’.
What would happen if we changed the amount entered in cell C2 from ’10000’ to ’15000’ as suggested above?
Obviously, our calculation defined by our formula would not automatically change, and we would have to edit
the formula by hand. Imagine if the cell we modified was referenced by ten separate formulas or functions.
That would be a lot of editing.
In summary, automatic calculation is activated, Excel automatically recalculates the result of any formulas
as cell entries change. Automatic calculation also applies to functions, which we’ll be covering in the next
section.
Side note: You can check to make sure automatic calculation is turned on:
❼ Select Tools from the Options drop menu.
SECTION 2. FORMULAS AND FUNCTIONS 4
1. Range:
In the function practice exercise, we used a union reference when we typed ’=AVERAGE(B2, C2)’ in cell
D2, but we could have used a range reference instead: ’=AVERAGE(B2:C2)’.
SECTION 2. FORMULAS AND FUNCTIONS 5
2.2.2 Autosum
You can use the Autosum icon on the standard toolbar, which automatically adds the contents of a cluster
of adjacent cells.
❼ Select the cell that the sum will appear in that is outside the cluster of cells whose values will be added.
❼ Click the Autosum button (Greek letter sigma, Σ).
❼ Highlight the group of cells that will be summed.
❼ Press the Enter key on the keyboard or click the green check mark on the formula bar.
→ Practice Exercise: In the Costs for the Fall and Spring semester spreadsheet, calculate the Total Cost
for the the Spring semester using the Autosum icon.
❼ Click on cell C6 to activate it.
❼ Click the Autosum button.
❼ Highlight cells C2 through C5.
❼ Press Enter.
From the Paste Function dialog box, browse through the functions by clicking in the Function category
menu on the left and select from the Function name choices on the right.
❼ As each function name is highlighted a description and example of use is provided below the two boxes.
Clicking OK to select the function, opens the Function Arguments dialog box, which allows you to choose the
cells that will be included in the function.
❼ As in the last previous function, we can type the cell addresses in the necessary argument boxes.
❼ We can also enter the cells using the point mode (i.e., the left mouse button).
– Click and drag across a group of cells to enter a range of cells addresses.
– Use the Ctrl key and mouse to enter a union of cells addresses.
❼ Excel may automatically select cells for you, but you can delete those selected cells from the argument
box and enter the desired cell addresses.
SECTION 2. FORMULAS AND FUNCTIONS 6
Click OK when all the cells for the function have been entered into the necessary argument boxes.
→ Practice Exercise: In the Costs for the Fall and Spring semester spreadsheet, calculate the average Housing
cost for the Fall and Spring semesters using the Function Wizard and the AVERAGE function.
The following table contains the definitions and examples of several other available functions:
As we’ve seen, the best way to construct a formula or a function is to use cell references (i.e., use the cell
addresses) instead of typing actual numbers. In turn, this enables Excel to automatically update the results
of the formulas and functions when you change the values in the cells referenced.
Using cell references in formulas and functions is also important when you copy a formula or function. When
you copy the formula or function, the cell addresses will be copied in a particular way depending on how
the cells are referenced. Specifically, Excel records cell addresses in formulas and functions in three different
ways:
❼ Relative cell reference.
❼ Absolute cell reference.
❼ Mixed cell reference.
As we saw in our first lecture, several different ways exist to copy and paste a formula or a function:
❼ Use Copy and Paste from the Edit drop menu.
❼ Use the Ctrl+C and Ctrl+P keyboard shortcuts.
❼ Use the Autofill feature – activate the cell containing the formula or function by selecting it and drag
the Autofill handle down the column or across the row to fill in the remaining cells.
Copying formulas and functions instead of typing them into a new cell obviously avoids typing errors and
will save you time.
7
SECTION 3. COPYING FORMULAS/FUNCTIONS AND CELL REFERENCES 8
→ Practice Exercise: Let’s work with a new data example. Enter the following information into a blank
worksheet (ignore any formatting) in columns A, B, and C, and in rows 1 through 5. Then define a formula
in cell D2 to calculate the Cost of the group of Biology Textbooks, and copy the formula into cells D3 through
D5.
Textbook Quantity Price
Biology 4 99.99
Chemistry 2 79.95
Calculus 7 65.99
English 12 49.99
❼ The most logical solution would be to type the formula ’=B2*C2’ into cell D2. We may also want to
add the column label of Cost into cell D1. We can then use one of the methods mentioned above to
copy and paste the formula into the desired cells.
Compare the formulas in cells D2 through D5 by looking in the Formula bar. You’ll notice that the for-
mulas are the same except that the row numbers were automatically changed (from 2 through 5) because of
the relative cell referencing.
→ Practice Exercise: In the Costs for the Fall and Spring semester spreadsheet, copy the AVERAGE function
in cell D3, which calculates the average Housing cost for the Fall and Spring semesters, to cell D4 through
D6. Compare the copied functions by looking in the Formula bar.
An absolute cell reference is most often used when you want to use a constant in a formula or function.
❼ Advantage of placing the constant in its own cell and using an absolute cell reference: Referencing the
cell containing the constant in all the desired formulas and functions with an absolute cell reference
allows you to change the value of the constant in the one cell and all the formulas and functions
absolutely referencing the cell will be automatically updated. This is much better than typing the
actual value of the constant into the formulas or functions.
→ Practice Exercise: The following exercise demonstrates what happens when you don’t use a absolute cell
reference when you should.
1. Create the following spreadsheet in columns A through F and rows 1 through 4 (ignore any formatting).
2. In cell D2, calculate the Total of Trial 1 and Trial 2 for subject A using the SUM function.
3. Use the Autofill feature to copy the function for subjects B and C.
4. In cell E2 enter the formulas ’=D2*F2’ either by typing or by using the point mode.
5. Use the Autofill feature to copy the formula for subjects B and C.
❼ NOTE: The results of the formula for subjects B and C are incorrect. This is because the formula
referenced the CONSTANT using a relative cell reference.
6. Edit the formula in cell E2 to reference the CONSTANT using an absolute cell reference: ’=D2*$F$2’.
7. Use the Autofill feature to copy the correct formula for subjects B and C.
❼ NOTE: The results of the formula for subjects B and C are now correct. The Total value adjusts
because of the relative cell reference, but the CONSTANT value does not because of the absolute
cell reference.
❼ EXAMPLE: In the formula ’=A$1 + $B2’, the row of cell A1 is fixed and the column of cell B2 is fixed.
As with absolute cell referencing, you need to add a dollar sign (’$’), but only in front of the column
OR row identifier for the cell referenced.
As before, you can add the dollar sign (’$’) automatically by using the F4 keyboard shortcut:
When you perform calculations in Excel, be careful of precedence. Excel follows the standard order of
operations when it performs all calculations:
3. Multiplication and Division: Performed in the order which they occur (from left to right).
4. Addition and Subtraction: Performed in the order which they occur (left to right).
In general, calculations are performed from left to right, with multiplication and divisions performed be-
fore addition and subtraction.
→ Practice Exercise: Let’s enter a dummy formula into cell A8 in the Costs for the Fall and Spring semester
spreadsheet.
❼ Type ’=3+3+12/2*4’ into cell A8.
– Excel divides 12 by 2, multiplies the answer by 4, adds 3, and then adds another 3, which results
in 30.
❼ To name a range, select the cells to be included in the range, and then type a name in the Name Box
located above the worksheet.
❼ Recall, the Name Box normally contains the address if the current cell.
10
SECTION 4. OTHER TOPICS TO CONSIDER 11
→ Practice Exercise: In the Costs for the Fall and Spring semester spreadsheet:
1. Delete the formula calculating the Total Cost for the Fall semester in cell B6.
3. Type ’Fall’ in the Name box and press Enter – the range of numbers is now identified by the name Fall.
4. Type ’=SUM(Fall)’ in cell B6.