spreadsheet_notes

advertisement
Spreadsheet Tips
SPREADSHEET NOTES
E
xcel is a spreadsheet program. Spreadsheet programs are model-building tools. You set
relations between cells by writing formulas, and see what happens when you change
values of variables. If, for example, you enter a formula like
=A1
in cell C1, you ask the program to display in C1 the value that is in A11.
When entering formulas, it is easy to run into trouble if you write a cell address manually
(you may mistype the address). It is safer to let the program write the address by pointing to
it, using either the keyboard or the mouse. This method is especially handy when entering
an address in another sheet. Another common mistake to watch out: When entering an
address in another worksheet, do not unnecessarily move from one sheet to the other. For
example, you are entering a formula in a cell in Sheet1 and you refer to a cell in Sheet2, like
=Sheet2!A1. Now, if you move back to Sheet1 before completing the formula, the formula
bar will change to =Sheet1!A1, which is incorrect.
Arithmetic operations and their operators:
Operation
Sign
Name
Addition
+
Plus
Subtraction
-
Minus
Multiplication
*
Asterisk
Division
/
Slash
Exponentiation
^
Caret
When you need to enter a value (numeric or alphanumeric) more than once, make sure that
they are interrelated with a formula. Otherwise, if one of them is changed, the others will
remain unchanged.
Below are some common arithmetic operators:
Conventional notation
A1 + B1
A1 – B1
A1 x B1
A1
B1
A1B1
B1 A1
Excel notation
=A1+B1
=A1-B1
=A1*B1
=A1/B1
=A1^B1
=A1^(1/B1)
1
This is not as redundant a formula as it may look: If you want the same variable to appear in
more than one cell, then never enter it more than once; enter it only once, and in the other cells
where you want it to be displayed, enter a formula that equates it to the cell of original entry, like the
first formula above.
1/10
Spreadsheet Tips
You can test a condition, like whether values of two cells are equal, with a formula like
=A1=B1
in cell C1. It reads like “is the value in cell A1 equal to the value in cell B1?” The formula will
return either TRUE or FALSE, as illustrated below:
A
1
2
B
25
25
E
C
50 FALSE
50 TRUE
Formulas in column C
=A1=B1
=A2<B2
xcel has predefined formulas, which are called functions. Their general syntax is
=function(arguments). For example, if you want to add the values in A1 through A4,
instead of entering the following formula,
=A1+A2+A3+A4
you can use the SUM() function:
=SUM(A1:A4) (Remember that you don't need to write addresses: after typing "=SUM(",
you select the cells to be added, so that the program supplies the address entry.). Likewise,
if the cells to be added are A1 through A4 and C1 through C5, the formula will be
=SUM(A1:A4,C1:C5). There are two arguments here, two ranges of cells to be summed; the
arguments are separated with commas.
o COUNT() function will return the number of cells that contain numeric values. For
example (remember that dates are numeric data):
A
1
2
3
Formula in A4
25.12.2006
15
abc
4
2 =COUNT(A2:A4)
Extensions of SUM() and COUNT() functions are SUMIF() and COUNTIF() functions. Suppose
we have the following table:
A
B
C
Name
Gender
Reading
capacity
1
2
3
Ece
Female
6
Emre
Male
2
4
Merve
Female
7
5
Yalçın
Male
1
Based upon this, the following table displays the number of each gender (COUNTIF()) and
the total reading capacity (whatever that means) of each gender:
E
1
2
F
G
Male
2
3
Female
2
13
The formula in F1 is
=COUNTIF($B$2:$B$5,E1)
2/10
Spreadsheet Tips
which can be read as: “how many cells in the range B2-B5 have the value of E1 (male) in
them?”
The formula in G1 is
=SUMIF($B$2:$B$5,E1,$C$2:$C$5)
which can be read as: “what is the sum of the range C2-C5 which meet the criteria of E1
(male) in the range B2-B5?”
RAND() is another function. It generates a random value between 0 and 1. If you want a
random value between 0 and 10, the formula to enter is: =RAND()*10.
AVERAGE() is another useful function.
See the following example:
A
B
Formulas in column B
1 Value 1 Maximum
2
100 =MAX(A2,100)
2
500
500 =MAX(A3,100)
3
100 =MAX(A4,100)
4
Here we want the formula to return either 100 or the value in column A (if there is one),
whichever is greater. In the first 2 rows, the formula works, but in the third we have 100
although there is no value in A. Of the many IS functions Excel provides, see how ISBLANK
function works:
A
B
Formulas in column B
2
=ISBLANK(A1)
3
=ISBLANK(A2)
4
=ISBLANK(A3)
You could read the formula in B1 as “is the cell A1 blank?” It returns TRUE if it is empty,
FALSE if the cell has either a numeric or alphanumeric value. You can use this function within
an IF function, like:
2
500
FALSE
FALSE
TRUE
A
B
Formulas in column B
Maximum
1 Value 1
2
100 =IF(ISBLANK(A2),"",MAX(A2,100))
2
500
500 =IF(ISBLANK(A3),"",MAX(A3,100))
3
4
=IF(ISBLANK(A4),"",MAX(A4,100))
The formula in B2 can be read as: “If there is no value in A2, that is, if it is empty, then return
an empty cell, otherwise (if it is not blank), return 100 or the value in A2, whichever is
greater.”
By inserting this condition-testing into an IF function, you can let the program take one
action if the condition holds (TRUE) and another action if it fails (FALSE). In Excel’s help,
syntax of IF is given as:
IF(logical_test,value_if_true,value_if_false)
which could be stated as
IF(condition,then,else)
Below are a few examples:
A
1
B
25
C
Formulas in column C
50 not equal =IF(A1=B1,"equal","not equal")
3/10
Spreadsheet Tips
2 Ali
Ali
equal
=IF(A2=B2,"equal","not equal")
3
25
25
625 =IF(A3<B3,"",A4*B4)
4 Ali
Veli
=IF(A4<B4,"",A5*B5)
Note that Excel applies left-alignment for alphanumaric values and right-alignment for
numeric values.
If you need to take actions for more conditions, you can embed IF functions within other IF
fınctions, replacing either the 2nd or 3rd parameters. See the following examples:
A
B
Formula in column B
1 Annual Income Tax Rate
2
15000000000
0.1 =IF(A2<10000000000,0,IF(A2<20000000000,0.1,0.2))
Formula in B2 can be read as: If A2 is less than 10 million, then return 0 in B2; otherwise (A2
is equal to or more than 10 million), if A2 is less than 20 million, then return 0.1 in B2;
otherwise (A2 is equal to or greater than 20 million, which is the only remaining case), put
0.2 in B2.
Excel allows up to seven nested IFs, but a formula will become very long and complicated
before you reach that limit. Instead of nesting too many IFs, it may be a better idea to use a
vertical or horizontal lookup table along with VLOOKUP or HLOOKUP function. To be able to
use VLOOKUP function, we first need to construct a vertical table, where the first column
has the values we are looking for. For example, the lookup table in A5:B9 below can be read
as follows: Annual incomes between 0 and 10 billion (not included) is subject to 0% tax rate;
incomes between 10B and 20B (not included) are subject to 10% rate; incomes between 20B
and 40B (not included) are subject to 20% rate, those between 40B and 80B (not included)
are subject to 30% rate, and incomes of 80B and above are subject to 40% tax rate.
Compare the formulas in B2 and B3, which do the same thing in the following example,
where the range A5:B9 has the (vertical) lookup table
A
B
Formulas in column B
1 Annual Income Tax Rate
15,000,000,000
%10 =IF(A3<10000000000,0,IF(A2<20000000000,0.1,IF(A2<400
2
00000000,0.2,IF(A2<80000000000,0.3,0.4))))
3
15,000,000,000
%10
=VLOOKUP(A3,A5:B9,2,TRUE)
4
0
%0 The greyed range has the vertical lookup table.
5
10,000,000,000
%10
6
20,000,000,000
%20
7
40,000,000,000
%30
8
80,000,000,000
%40
9
No matter how many more conditions you add to be tested, the VLOOKUP function will
remain the same compact size; you only need to change the range (address) of the table.
The syntax of VLOOKUP function is
VLOOKUP(P1,P2,P3[,P4])
Where P1-P4 are the four parameters. They are

P1 = what value to look up for in the first column of the lookup table

P2 = the address of the lookup table

P3 = the column number we want to be returned

P4 = whether we want ranges (TRUE) or exact
values (FALSE). The forth parameter is optional,
in the sense that, if you want ranges, you don’t
4/10
NOTE: When constructing a
lookup table, make sure that the
first column has the values that
are being looked up.
Spreadsheet Tips
need to enter it. Thus, the last formula above, =VLOOKUP(A3,A5:B9,2,TRUE) is
equivalent to =VLOOKUP(A3,A5:B9,2). In the above example, we do want ranges,
because the annual income figure can be any non-negative value.
If/when we want ranges, we must make sure that the first column of the vertical lookup
table (which has the searched values)
 starts with the lowest possible value, and
 is sorted in ascending order.
If we want exact matches (range is FALSE), then the above two rules do not apply; the
program is going to search each value in the first column anyway.
O
ne can get logical results (TRUE/FALSE) like:
1
2
A
B
C
Formulas in column A
TRUE
FALSE
1
1
1
2
=B1=C1
=B2=C2
This is especially useful with what Excel calls “information functions” like ISBLANK() and
ISERROR(). Take the following example:
1
2
3
A
B
C
Formulas in column C
12
2
6
#DIV/0!
#DIV/0!
=A1/B1
=A2/B2
=C1+C2
Total
Here, since there is no variable entered in C2, the program returns a “division by zero” error.
It, in turn, causes the sum formula in C3 to have an error. In such cases, we can use ISBLANK
function to test whether there is a value in a cell:
1
2
3
A
B
C
Formulas in column C
12
2
6
=IF(ISBLANK(B1),"", A1/B1)
=IF(ISBLANK(B2),"", A2/B2)
=C1+C2
Total
6
The formula in C1 can be read as: “If there is no value in B1, then leave it empty, otherwise
divide the value in A1 by it.” (This is just an example; you could, of course, test whether B1 is
zero or not.)
ISBLANK() is classified as an information function. Another useful information function is
ISERROR(), which refers to any error value. For example, if we have a lookup function
whereby we want an exact value (4th argument is FALSE), then an entry that does not exist in
the first column of a vertical lookup value will return #N/A error. Our user may not
understand what this error message means. To replace it with a more meaningful message,
we may want to use the ISERROR() function: Next to the column we have the lookup
function, we can enter the following formulas:
=IF(ISERROR(previous column), “Entry not found”, previous column)
Now, these two columns will return the same, correct value if the entry is correct, and, if it is
incorrect, then the first column will return #N/A and the second one will return “Entry not
5/10
Spreadsheet Tips
found”. Since the first column has an intermediary formula, we may hide it, so that the user
will not be confused by it (to hide a column: Home / Format / Hide & Unhide..)
B
y default, an address entered in a formula is a relative address. Thus, if you enter
=A1+B1 in cell C1, A1 and B1 actually do not mean A1 and B1! To verify this, copy the
formula in C1 to C2: you will see that A1 becomes A2 and B1 becomes B2. If A1 and B1
had meant A1 and B1, they should remain the same when copied.
A
B
C
Formulas in column C
1
0
=A1+B1
2
0
=A2+B2
They instead mean a location relative to the location of the cell they are entered in. Thus,
since the formula is in C1, A1 means the cell in the same row and 2 columns to the left; B1
means the cell in the same row and 1 column to the left. The same relation holds when they
are copied to C2.
Although relative addressing serves our purposes most of the time, there are cases when it
won’t work.
A
B
1
Year
Price
2
2000
1,000
3
2001
1,100
4
2002
1,210
C
Formulas in column B
Rate of
increase
10%
=B2*(1+C2)
In a formula in B3, B2 means “the same column, one row above” and C2 means “one column
to the right and one row above”. The same relative relation will be maintained when the
formula is copied to C4 => =B3*(1+C3). Here, we do not want C2 to change when copied, so
we need to make the address absolute. On the formula bar, while the pointer is on C2, we
press F4 key. C2 becomes $C$2. This means both the column (C) and the row (2) are
absolute, that is, they don’t depend on the location of the formula they are in (C3).
Wherever we copy that formula, $C$2 will remain the same.
We have seen that, while the pointer is on an address like C2, pressing F4 will change it to
$C$2. Pressing F4 once more will cycle the address to C$2, then to $C2, and back to C2. The
$ sign in front of a column letter or a row number will make that part of the address
absolute.
In the following example, values for year 2000 are variable (1,000). Prices of 2 goods, G1 and
G2 increase by 5% and 10%, respectively. To find the values for 2001 and 2002, entering one
formula in B4 is sufficient =B3*(1+B$2); it is then copied to B4:C5.
1
A
B
C
Year
G1
G2
5%
10%
2
Formulas in
column B
Formulas in
column C
3
2000
1,000
1,000
4
2001
1,050
1,100
=B3*(1+B$2)
=C3*(1+C$2)
5
2002
1,103
1,210
=B4*(1+B$2)
=C4*(1+C$2)
6/10
Spreadsheet Tips
MISCELLANEOUS TOPICS
Defining range names: A cell or a range of cells can be given a name. The defined name
can be used in a formula so that it may be easier to understand than an address. For
example, if you define columns A and B as Quarter1 and Quarter2, in cell C1, you can enter
a formula, = Quarter1+Quarter2 instead of =A1+B1. Defined names also make navigation
within workbooks easier. When you press Ctrl-G (Go to key), a list of defined names appears.
Double clicking one will take you to the defined range. Another use of defined names is
creating hyperlinks. To define a name, use Formulas=>Define Name. To edit defined names,
use Formulas=>Manage Names.
Duplicate entries: A variable should be entered only once in a workbook. If a variable
needs to appear more than once on a workbook, each extra entry should be related to the
first entry with a formula, so that, if the variable is altered, the change is automatically
reflected on other appearances.
Alphanumeric constants: We generally do not want to have constants (numeric or
alphanumeric) in fornulas because they are relatively harder to change, and they reduce
transparency. If, for some reason, we want to enter alphanumeric constants in formulas,
they need to be enclosed by quotes (“) (not two apostrophies). For example:
1
2
3
4
B
Grade
AA
C
Assessment
formula in C2:
good
=IF(ISBLANK(B3),"",IF(B3="AA","good","bad"))
BA
bad
The formula in cell C2 does this: If the cell left is blank, then return blank (leave it blank (two
quotes)), otherwise, if that cell has AA, then return good, otherwise return bad.
7/10
Spreadsheet Tips
DESIGN AND FORMAT
Design of a model is how it works and how it looks as a whole. A well-designed model will
have an easy-to-follow logic. Its variables will be appropriately placed with easy-tounderstand labels (explanations of what they stand for).
☺ An Excel document is called a workbook, and it comprises of worksheets, named as
Sheet1, Sheet2, etc. To facilitate navigation, it is a good idea to name them, so that
the user has an idea about the content of each sheet. To rename a worksheet, Home /
Format / Rename Sheet, or select the tab (for example, Sheet1), then either right-click
Rename or double-click the tab.
☺ In Excel numeric values are right-aligned and alpha-numeric values are left-aligned by
default (their automatic settings). To change these settings, choose Home from the
top menu; click the button right of Alignment tool, then Text alignment, Horizontal.
☺ To change the font attribute of part of the content of a cell, select that part (either in
the formula bar or in the cell), then Font.
☺ In a range of cells, to highlight those cells that satisfy a certain condition, first
select all the cells that are involved, choose Home from the top menu; click the
button right of Conditional Formatting => Select a Rule Type => Format only
cells that contain, then Edit the Rule Description..
☺ To change the look of a percentage, for example, .5, so that it looks like 50%,
choose Home from the top menu; click the button right of Number 
Percentage.
☺ In tables, category headings
should somehow be
distinguished (by changing font
types or sizes) from the items
they represent. If a column
heading is long, wrapping text
might be better than shortening
or abbreviating it to the extent that it loses its meaning. By spanning long titles in 2 or
more rows, you can also save horizontal space: Home  Alignment  Wrap text.
☺ There is another way which enables to wrap text at a specific point in a cell: click on
the formula bar, click the location where you want to break the line in the cell, and
then press ALT+ENTER
☺ While gridlines (the vertical and horizontal lines that define cells) are useful during our
work with spreadsheets, we may want to remove them when our work is completed,
so that our tables stand out. To make gridlines visible or invisible, choose View from
the top menu; in the Show/Hide tool, select or deselect Gridlines.
8/10
Spreadsheet Tips
☺ To have lines at specific locations, like cells within a table, select the desired range,
Home  Format  Format Cells  Border.
☺ To indent cells, Home => Alignment => Horizontal => Left (Indent),
then choose a value at Indent.
9/10
Item 1
Item 2
Subitem 1
Subitem 2
Item 3
Spreadsheet Tips
TROUBLESHOOTING
2.1E+09
3E+09
1E+09
8E+08
6.9E+09
These cells, because of their insufficient width, display
numeric values in scientific notation or exponential format .
For example, the first number is 2.1 times E to the power of
9. If you don’t want this notation, increase the column
width.
2100000000
3000000000
1000000000
800000000
6900000000
-------------------This, again, means that column width is not sufficient to
2,100,000,000
accommodate the number (probably due to its format).
3,000,000,000
Just increase the column width: Home / Format / Column
Width, or place mouse pointer on the line separating column letters so that the pointer
changes to a cross and drag the pointer to either side to adjust coulmn width. You can also
double-click that cross so that column width will “Autofit” (be as wide as the widest cell).
##########
##########
--------------------
10/10
Download