Excel Forms Funcs
Excel Forms Funcs
7.1 Formulas
To add a formula to a worksheet, you enter it into a cell. You can delete, move, and
copy formulas just like any other item of data. Formulas use arithmetic operators to work
with values, text, worksheet functions, and other formulas to calculate a value in the cell.
A formula entered into a cell can consist of any of the following elements:
o =150*.05 Multiplies 150 times .05. This formula uses only values and isn’t all
that useful.
o =A1+A2 Adds the values in cells A1 and A2.
o =Income–Expenses Subtracts the cell named Expenses from the cell named
Income.
o =SUM(A1:A12) Adds the values in the range A1:A12.
o =A1=C12 Compares cell A1 with cell C12. If they are identical, the formula
returns TRUE; otherwise, it returns FALSE.
Excel lets you use a variety of operators in your formulas. The following is a list
of the operators that Excel recognizes. In addition to these, Excel has many built-in
functions that enable you to perform more operations.
Operator Name
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponentiation
& Concatenation
= Logical comparison (equal to)
> Logical comparison (greater than)
The following are some additional examples of formulas that use various
operators.
You use parentheses to override Excel’s built-in order of precedence. You can
also nest parentheses in formulas, which means putting parentheses inside of parentheses.
If you do so, Excel evaluates the most deeply nested expressions first and works its way
out.
As mentioned earlier, a formula must begin with an equal sign to inform Excel
that the cell contains a formula rather than text. Basically, two ways exist to enter a
formula into a cell: enter it manually or enter it by pointing to cell references.
You simply type an equal sign (=), followed by the formula. As you type, the
characters appear in the cell and in the formula bar. You can, of course, use all the normal
editing keys when entering a formula.
The other method of entering a formula still involves some manual typing, but
you can simply point to the cell references instead of entering them manually. For
example, to enter the formula =A1+A2 into cell A3, follow these steps:
Pointing to cell addresses rather than entering them manually is usually more
accurate and less tedious.
Excel includes the Formula Palette feature that you can use when you enter or edit
formulas. To display the Formula Palette, click the Edit Formula button in the Formula
bar (the Edit Formula button looks like an equal sign). The Formula Palette lets you enter
formulas manually or use the pointing techniques described previously. The Formula
Palette, shown in Figure 1, displays the result of the formula as it’s being entered. The
Formula Palette usually appears directly below the edit line, but you can drag it to any
convenient location.
Formulas can refer to cells in other worksheets —and the worksheets don’t even
have to be in the same workbook. Excel uses a special type of notation to handle these
types of references.
To use a reference to a cell in another worksheet in the same workbook, use the
following format:
SheetName!CellAddress
In other words, precede the cell address with the worksheet name, followed by an
exclamation point. Here’s an example of a formula that uses a cell on the Sheet2
worksheet:
This formula multiplies the value in cell A1 on the current worksheet by the value
in cell A1 on Sheet2.
=[WorkbookName]SheetName!CellAddress
In this case, the workbook name (in square brackets), the worksheet name, and an
exclamation point precede the cell address. The following is an example of a formula that
uses a cell reference in the Sheet1 worksheet in a workbook named Budget:
=[Budget.xls]Sheet1!A1
If the workbook name in the reference includes one or more spaces, you must
enclose it (and the sheet name) in single quotation marks. For example, here’s a formula
that refers to a cell on Sheet1 in a workbook named Budget For 1999:
You need to be able to distinguish between relative and absolute cell references.
By default, Excel creates relative cell references in formulas except when the formula
includes cells in different worksheets or workbooks. The distinction becomes apparent
when you copy a formula to another cell.
Relative References:
Figure 2 shows a worksheet with a formula in cell D3. The formula, which uses
the default relative references, is as follows:
=B3*C3*(1+B7)
When you copy this formula to the two cells below it, Excel doesn’t produce an
exact copy of the formula; rather, it generates these formulas:
Excel adjusts the cell references to refer to the cells that are relative to the new
formula. Think of it like this: The original formula contained instructions to multiply the
value two cells to the left by the value one cell to the left. When you copy the cell, these
instructions get copied, not the actual contents of the cell.
Absolute References:
=(B3*C3)*(1 + $B$7)
Notice that the reference to cell B7 has dollar signs preceding the column letter
and the row number. These dollar signs indicate to Excel that you want to use an absolute
cell reference. When you copy this formula to the two cells below, Excel generates the
following formulas:
In this case, the relative cell references were changed, but the reference to cell B7
wasn’t changed, because it’s an absolute reference.
7.2 Functions
Functions, in essence, are built-in tools that you use in formulas. They can make
your formulas perform powerful feats and save you a lot of time. Functions can do the
following:
=(A1+A2+A3+A4+A5+A6+A7+A8+A9+A10)/10
Not very pretty, is it? Even worse, you would need to edit this formula if you
added another cell to the range. You can replace this formula with a much simpler one
that uses one of Excel’s built-in worksheet functions:
=AVERAGE(A1:A10)
What if you need to determine the largest value in a range? A formula can’t tell
you the answer without using a function. Here’s a simple formula that returns the largest
value in the range A1:D100:
=MAX(A1:D100)
Functions also can sometimes eliminate manual editing. Assume that you have a
worksheet that contains 1,000 names in cells A1:A1000, and all the names appear in all
capital letters. Your boss sees the listing and informs you that the names will be mail-
merged with a form letter and that all-uppercase is not acceptable; for example,
JOHN F. CRANE must appear as John F. Crane. You could spend the next several hours
reentering the list —or you could use a formula like the following, which uses a function
to convert the text in cell A1 to proper case:
=PROPER(A1)
Enter this formula once in cell B1 and then copy it down to the next 999 rows.
Then, select B1:B1000 and use the Edit -> Paste Special command (with the Values
option) to convert the formulas to values. Delete the original column, and you’ve just
accomplished several hours of work in less than a minute.
One last example should convince you of the power of functions. Suppose that
you have a worksheet that calculates sales commissions. If the salesperson sold more than
$100,000 of product, the commission rate is 7.5 percent; otherwise, the com-mission rate
is 5.0 percent. Without using a function, you would have to create two different formulas
and make sure that you use the correct formula for each sales amount. Here’s a formula
that uses the IF function to ensure that you calculate the correct commission, regardless
of the sales amount:
=IF(A1<100000,A1*5%,A1*7.5%)
In the preceding examples, you may have noticed that all the functions used
parentheses. The information inside the parentheses is called an argument. Functions
vary in how they use arguments. Depending on the function, a function may use:
o No arguments
o One argument
o A fixed number of arguments
o An indeterminate number of arguments
o Optional arguments
The RAND function, which returns a random number between 0 and 1, doesn’t
use an argument. Even if a function doesn’t use an argument, however, you must still
provide a set of empty parentheses, like this:
=RAND()
If a function uses more than one argument, you must separate each argument by a
comma. The examples at the beginning of the chapter used cell references for arguments.
Excel is quite flexible when it comes to function arguments, however. An argument can
consist of a cell reference, literal values, literal text strings, or expressions.
As you’ve seen, functions can use cell or range references for their arguments.
When Excel calculates the formula, it simply uses the current contents of the cell or range
to perform its calculations. The SUM function returns the sum of its argument(s). To
calculate the sum of the values in A1:D20, you can use:
=SUM(A1:A20)
And, not surprisingly, if you’ve defined a name for A1:A20 (such as Sales), you
can use the name in place of the reference:
=SUM(Sales)
In some cases, you may find it useful to use an entire column or row as an
argument. For example, the formula that follows sums all values in column B:
=SUM(B:B)
This technique is particularly useful if the range that you’re summing changes (if
you’re continually adding new sales figures, for instance). If you do use an entire row or
column, just make sure that the row or column doesn’t contain extraneous information
that you don’t want included in the sum. You might think that using such a large range (a
column consists of 65,536 cells) might slow down calculation time —this isn’t true.
Excel’s recalculation engine is quite efficient.
Literal arguments:
A literal argument is a value or text string that you enter into a function. For
example, the SQRT function takes one argument. In the following example, the formula
uses a literal value for the function’s argument:
=SQRT(225)
Using a literal argument with a simple function like this one defeats the purpose
of using a formula. This formula always returns the same value, so it could just as easily
be replaced with the value 15. Using literal arguments makes more sense with formulas
that use more than one argument. For example, the LEFT function (which takes two
arguments) returns characters from the beginning of its first argument; the second
argument specifies the number of characters. If cell A1 contains the text
Budget, the following formula returns the first letter, or B:
=LEFT(A1,1)
Expressions as arguments:
=SQRT((A1^2)+(A2^2))
This formula uses the SQRT function, and its single argument is the following
expression:
(A1^2)+(A2^2)
When Excel evaluates the formula, it starts by evaluating the expression in the
argument and then computes the square root of the result.
You have two ways available to enter a function into a formula: manually or by
using the Paste Function dialog box.
If you’re familiar with a particular function —you know how many arguments it
takes and the types of arguments —you may choose simply to type the function and its
arguments into your formula. Often, this method is the most efficient.
If you omit the closing parenthesis, Excel adds it for you automatically. For
example, if you type =SUM(A1:C12 and press Enter, Excel corrects the formula by
adding the right parenthesis.
Pasting a Function:
Formula Palette assists you by providing a way to enter a function and its
arguments in a semi-automated manner. Using the Formula Palette ensures that the
function is spelled correctly and has the proper number of arguments in the correct order.
To insert a function, start by selecting the function from the Paste Function dialog
box, shown in Figure 4. You can open this dialog box by using any of the following
methods:
The Paste Function dialog box shows the Function category list on the left side of
the dialog box. When you select a category, the Function name list box displays the
functions in the selected category.
The Most Recently Used category lists the functions that you’ve used most
recently. The All category lists all the functions available across all categories. Use this
if you know a function’s name, but aren’t sure of its category.
When you locate the function that you want to use, click OK. Excel’s Formula
Palette appears, as in Figure 5, and the Name box changes to the Formula List box. Use
the Formula Palette to specify the arguments for the function. You can easily specify a
range argument by clicking the Collapse Dialog button (the icon at the right edge of each
box in the Formula Palette). Excel temporarily collapses the Formula Palette to a thin
box, so that you can select a range in the worksheet.
When you want to redisplay the Formula Palette, click the button again.
The Formula Palette usually appears directly below the formula bar, but you can move it
to any other location by dragging it.
INT
=INT(412.98)
RAND
=RAND()*12
The following formula generates a random integer between two values. The cell
named Lower contains the lower bound, and the cell named Upper contains the upper
bound:
=INT((Upper-Lower+1)*RAND()+Lower)
ROUND
The ROUND function rounds a value to a specified digit to the left or right of the
decimal point. This function is often used to control the precision of your calculation.
ROUND takes two arguments: the first is the value to be rounded; the second is the digit.
If the second argument is negative, the rounding occurs to the left of the decimal point.
For example,
=ROUND(123.456, 0) = 123
=ROUND(123.456, 1) = 123.5
=ROUND(123.456, 2) = 123.46
SIN
The SIN function returns the sine of an angle. SIN takes one argument —the
angle expressed in radians. To convert degrees to radians, use the RADIANS function (a
DEGREES function also exists to do the opposite conversion). For example, if cell
F21 contains an angle expressed in degrees, the formula that follows returns the sine:
=SIN(RADIANS(F21))
SQRT
The SQRT function returns the square root of its argument. If the argument is
negative, this function returns an error. The example that follows returns 32:
=SQRT(1024)
To compute a cube root, raise the value to the 1 /3 power. The example that
follows returns the cube root of 32768 —which is 32. Other roots can be calculated in a
similar manner.
=32768^(1/3)
SUM
If you analyze a random sample of workbooks, you’ll likely discover that SUM is
the most widely used function. It’s also among the simplest. The SUM function takes
from 1 to 30 arguments. To calculate the sum of three ranges (A1:A10, C1:10, and
=SUM(A1:A10,C1:10,E1:E10)
The arguments don’t have to be all the same type. For example, you can mix and
match single cell references, range references, and literals, as follows:
=SUM(A1,C1:10,125)
SUMIF
The SUMIF function is useful for calculating conditional sums. SUMIF takes
three arguments. The first argument is the range that you’re using in the selection criteria.
The second argument is the selection criteria. The third argument is the range of values
to sum if the criteria are met. Suppose in a weekly timber production data set Column C
contains the end date of the week and Column M is the weekly production. If we would
like to sum this logger’s timber production in 1999, the following function can be used:
=SUMIF(C3:C136, "<31-dec-99",L3:L136) –
SUMIF(C3:C136, "<31-dec-98",L3:L136)
Although Excel is primarily known for its numerical prowess, it has 23 built-in
functions that are designed to manipulate text.
LEFT
=LEFT(“B.B. King”)
=LEFT(“Albert King”,5)
LEN
The LEN function returns the number of characters in a string of text. For
example, the following formula returns 12:
=LEN(“Stratocaster”)
If you don’t want to count leading or trailing spaces, use the LEN function with a
nested TRIM function. For example, if you want to know the number of characters in the
text in cell A1 without counting spaces, use this formula:
=LEN(TRIM(A1))
MID
The MID function returns characters from a text string. It takes three arguments.
The first argument is the text string. The second argument is the position at which you
want to begin extracting. The third argument is the number of characters that you want to
extract. If cell A1 contains the text Joe Louis Walker, the formula that follows returns
Louis:
=MID(A1,5,5)
REPLACE
The REPLACE function replaces characters with other characters. The first
argument is the text containing the string that you’re replacing. The second argument is
the character position at which you want to start replacing. The third argument is the
number of characters to replace. The fourth argument is the new text that will replace the
existing text. In the example that follows, the formula returns Albert Collins:
UPPER
=UPPER(A1)
Excel also has a LOWER function (to convert to lowercase) and a PROPER
function (to convert to proper case). In proper case, the first letter of each word is
capitalized.
The Logical category contains only six functions (although several other functions could,
arguably, be placed in this category). This section discusses three of these functions: IF,
AND, and OR.
IF
The IF function is one of the most important of all functions. This function can
give your formulas decision-making capability.
The IF function takes three arguments. The first argument is a logical test that
must return either TRUE or FALSE. The second argument is the result that you want the
formula to display if the first argument is TRUE. The third argument is the result that you
want the formula to display if the first argument is FALSE.
In the example that follows, the formula returns Positive if the value in cell A1 is
greater than zero, and returns Negative otherwise:
In our Christmas tree sale example, if B6 represents the sale amount, the
commission can be calculated as follows:
AND
The AND function returns a logical value (TRUE or FALSE) depending on the
logical value of its arguments. If all its arguments return TRUE, the AND function
returns TRUE. If at least one of its arguments returns FALSE, AND returns FALSE. In
the example that follows, the formula returns TRUE if the values in cells A1:A3 are all
negative:
=AND(A1<0,A2<0,A3<0)
The formula that follows uses the AND function as the first argument for an IF
function. If all three cells in A1:A3 are negative, this formula displays All Negative. If at
least one is not negative, the formula returns Not All Negative:
OR
The OR function is similar to the AND function, but it returns TRUE if at least
one of its arguments is TRUE; otherwise, it returns FALSE. In the example that follows,
the formula returns TRUE if the value in any of the cells—A1, A2, or A3—is negative:
=OR(A1<0,A2<0,A3<0)
Excel has 14 date and time relate d functions that allow you to manipulate these
types of data.
TODAY
The TODAY function takes no argument. It returns a date that corresponds to the
current date —that is, the date set in the system. If you enter the following formula into a
cell on January 5, 2003, the formula returns 1/5/2003:
=TODAY()
DATE
The DATE function displays a date based on its three arguments: year, month,
and day. This function is useful if you want to create a date based on information in your
worksheet. For example, if cell A1 contains 2003, cell B1 contains 1, and cell
C1 contains 4, the following formula returns 1/4/2003:
=DATE(A1,B1,C1)
DAY
The DAY function returns the day of the month for a date. If cell A1 contains the
date 1/5/2003, the following formula returns 5:
=DAY(A1)
TIME
The TIME function displays a time based on its three arguments: hour, minute,
and second. This function is useful if you want to create a time based on information in
your worksheet. For example, if cell A1 contains 10, cell B1 contains 25, and cell C1
contains 9, the following formula returns 10:25:09 AM:
=TIME(A1,B1,C1)
HOUR
The HOUR function returns the hour for a time. If cell A1 contains the time
10:25:09 AM, the following formula returns 10:
=HOUR(A1)
AVERAGE
The formula that follows returns the average of the values in the range A1:A100:
=AVERAGE(A1:A100)
If the range argument contains blanks or text, Excel doesn’t include these cells in
the average calculation. As with the SUM formula, you can supply any number of
arguments.
Excel also provides the MEDIAN function (which returns the middle-most value
in a range) and the MODE function (which returns the value that appears most frequently
in a range).
COUNTIF
The COUNTIF function is useful if you want to count the number of times that a specific
value occurs in a range. This function takes two arguments: the range that contains the
value to count and a criterion used to determine what to count. The COUNTIF function
is used in the formulas in column E. For example, the formula a cell below can count the
number of students with grade A.
=COUNTIF(B:B,A)
Notice that the first argument consists of a range reference for the entire column
B, enabling you to insert new names easily without having to change the formulas.
Use the MAX function to return the largest value in a range, and the MIN
function to return the smallest value in a range. Both MAX and MIN ignore logical
values and text. The following formula displays the largest and smallest values in a range
named Data; using the concatenation operator causes the result to appear in a single cell:
For example, if the values in Data range from 12 to 156, this formula returns
Smallest: 12 Largest: 156.
Class Exercises
(1) Lab 3.
References
Walkenbach, J. 1999. Microsoft Excel 2000 Bible. IDG Books Worldwide, Inc. Foster
City, CA.