Using Formulae and Functions in Microsoft Excel 2003
Using Formulae and Functions in Microsoft Excel 2003
Version 1.0
£1
Document code: Guide 35
Title: Using formulae and functions in Microsoft Excel 2003
Version: 1.0
Date: June 2006
Produced by: University of Durham Information Technology Service
Conventions:
In this document, the following conventions are used:
• A typewriter font is used for what you see on the screen.
• A bold typewriter font is used to represent the actual characters you type at
the keyboard.
• A slanted typewriter font is used for items such as filenames which you should
replace with particular instances.
• A bold font is used to indicate named keys on the keyboard, for example,
Esc and Enter, represent the keys marked Esc and Enter, respectively.
• A bold font is also used where a technical term or command name is used in
the text.
• Where two keys are separated by a forward slash (as in Ctrl/B, for example),
press and hold down the first key (Ctrl), tap the second (B), and then release
the first key.
Contents
1 Activate Excel.
2 From the File menu, select Open.
3 Click on the 6 to the right of the Look in: box.
4 Select the drive called workfile on ‘dudley’(T:).
5 Double-click on the folder its.
6 Double-click on the folder Excel.
7 Select the file Data_for_Calculations.xls and click the Open
button.
2 Press the Enter key (or one of the arrow keys) to lock the formula
in.
The cell B2 now contains a formula (=8+4), but the answer 12 is displayed
on-screen.
% Percent
They are listed in decreasing order of priority starting with negation which
has the highest priority (done first) and ending with comparison which has
the lowest (done last). If a formula contains operators with the same
priority, they are evaluated from left to right.
If you want to alter the order of evaluation, use parentheses (brackets) to
group expressions. Any parts of a formula that are in parentheses are done
first.
It is important to fully understand the order in which Excel carries out a
calculation.
Formula Answer
=(2*9)+12/(6-4) 24
=2*(9+12)/6-4 3
=(2*9+12)/6-4 1
=2*(9+12/(6-4)) 30
12 216
15 130
19 80
24 50
2 Click in B11 and note how Excel enters that reference into the
formula.
3 Type
+
4 Click in C11.
5 Press the Enter key.
6 Check that the answer is 228.
1 Click in D11.
2 Click the Copy button.
3 Click in D12.
4 Click on the Paste button (and ignore the smart tag if it appears).
Another way of copying is to use the Auto Fill handle:
1 Click in D12.
2 Move the cursor to the bottom right-hand corner of the cell. The
cursor shape will change to a small black plus (+) sign.
3 Hold down the mouse button, drag to cell D14 and then release the
button.
The formula in D11 was =B11+C11. When that was copied and pasted into
D12 it became =B12+C12. This is because Excel, unless instructed
otherwise, works with relative cell references. It thinks about where cells
are in relation to the cell in which the formula is entered.
1 Scroll down the worksheet until you can see the range B20:F24 with
data about VAT.
First, complete the VAT column:
1 Click in C21.
2 Type
=B21*F21
1 Double-click in C21.
2 Edit the formula to read
=B21*$F$21
3 Press Enter.
4 Copy the formula in C21 down to cell C24 and check that the
resulting values are correct.
Finally, complete the Cost with VAT column.
When entering formulae you can, if you wish, refer to cells in other
worksheets of the same workbook and to cells in other workbooks.
2 Adding numbers
There are a couple of easy ways of adding up a column of numbers. To
access some suitable data,
1 Click in D13.
2 Type
=SUM(D7:D11)
3 Functions
A function is a predefined formula that operates on one or more values and
returns one or more values.
You have seen how using the SUM function can save you a lot of typing.
For example, the formula
=SUM(A1:A15)
=A1+A2+A3+A4+A5+A6+A7+A8+A9+A10+A11+A12+A13+A14+A15
4 Mathematical functions
4.1 SUM
First add up the numbers in the Number of sneezes in eight hours
column of the Sneeze count for Monday table as follows:
1 Make sure that the Sneezes worksheet is still the active one.
2 Scroll up to the first table, Sneeze count for Monday.
3 Click in E13.
4 Click the Insert Function (fx) button, to the left of the formula bar.
The Insert Function dialog box will open.
7 Click on OK.
The Function Arguments dialog box appears. This contains one edit box
for each argument of your function.
Note how Excel has tried (but failed) to guess what you want to do and has
filled in the range C13:D13 (which would actually give the same result).
4.2 ROMAN
Suppose you want to convert some arabic numbers to roman (as text) but
you do not know whether there is a function that will do this.
1 Click in D32.
2 Click the Insert Function (fx) button.
3 In the Search for a function: box, type roman and click Go.
You will see the function name ROMAN listed in the Select a function:
pane, and, below that, a description of what that particular function does.
Throughout the rest of this document instructions will be given for entering
a function by typing it in, rather than by using the Insert Function dialog
box. You can use whichever method suits you best.
5 Text functions
So far, you have only dealt with numbers. There are several functions that
enable you to manipulate text strings, convert numeric entries into text
strings and convert numeric text entries into numbers.
To access some suitable data,
5.1 REPT
The REPT function gives you a quick way of filling a cell with a string of
characters repeated a specified number of times.
General form:
=REPT(text,number_of_times)
1 Click in B17.
2 Type
=rept(“please cut here.... ”,8)
5.2 TRIM
Leading or trailing spaces often prevent entries in a worksheet from sorting
correctly. The TRIM function eliminates leading, trailing, and extra blank
characters from a string, leaving only a single space between words.
General form:
=TRIM(text)
1 Click in B2 and look at the formula bar to see the extra spaces at the
beginning of the heading and between the words.
Now create a neater version on row 4.
2 Click in B4.
3 Type
=trim(b2)
and press Enter.
Note: The CLEAN function is similar to TRIM except that it deals with non-
printable characters like tabs and program-specific codes. This can be
particularly useful when dealing with data imported from other programs.
5.3 CONCATENATE
This function can be used to build up a long string from shorter strings. It
has the same effect as using the & operator mentioned in section 1.2.
General form:
=CONCATENATE(text1,text2,…)
1 Click in D7.
2 Type
=concatenate(c7,” “,b7)
=UPPER(text)
=LOWER(text)
=PROPER(text)
1 Click in E7.
2 Type
=proper(d7)
1 Click in C20.
2 Type
=upper(b20)
=”21/8/2000”-”18/7/2000”
6.5.1 TODAY
1 Click on the Date&Time tab of the workbook.
2 Click in C2.
6.5.2 NOW
Next try using a combined date and time format:
1 Click in C3.
2 Type
=now()
6.5.3 WEEKDAY
You may already know on which day of the week you were born. You can
check that using the function WEEKDAY.
1 Click in D16.
2 Type
=C16-B16
1 Click in D27.
2 Type
=min(D16:D25)
3 Click in D28.
4 Type
=minute(D27)
7 Logical functions
A logical value is either TRUE or FALSE.
There are six logical operators
= Equal to
1 Click in D7.
2 Type
=B2<4
1 Click in D8.
2 Type
=C2+C3>=8
1 Click in D9.
2 Type
=B4=”Hello”
or
=b4=”hello”
1 Click in D10.
2 Type
=C3=C4
7.2 IF
The IF function is very useful. It will carry out a conditional test and then
return one value if the condition is true but a different value if the condition
is false.
General form:
Example 2: Coding
This will return the value 1 if I4 contains M, otherwise it will return the value
0.
2 Click in I16.
3 Type
=if(H16>=200,”Pass”,”Fail”)
=IF(condition1,value1,IF(condition2,value2,value3))
TRUE value1
1 Click in K16.
2 Type
=if(I16=”Pass”,”No action”,if(H16>99,”Resit”,”Leave”))
General forms:
=AND(logical1,logical2,…,logical30)
=OR(logical1,logical2,…,logical30)
=NOT(logical1)
Formula Result
=AND(5>3,80<96) TRUE
=AND(2*24<67,7<0.34,4*4=2*8) FALSE
=OR(4*4=8*2,56>297) TRUE
=OR(2=3,3=4,4=5) FALSE
=NOT(5<3) TRUE
=NOT(6=6) FALSE
In your own worksheets, you will probably use logical conditions involving
some cell references rather than just values as above.
Have a look again at the table of examination results. Suppose that this
time a student is said to have passed if he/she gets at least 50 marks in
each module. (This means that good marks in one module cannot
compensate for poor marks in another.)
This definition of pass/fail is to be entered in the Result 2 column.
1 Click in J16.
2 Type
=if(and(D16>=50,E16>=50,F16>=50,G16>=50),”Pass”,”Fail”)
8 Statistical functions
Excel provides a great deal of assistance when it comes to analysing
statistical data. There are several built-in functions, a few of which will be
looked at in this document. In addition, the Analysis ToolPak can be used
(see section 16.6). This is an add-in module with a collection of functions
and tools that enable you to produce rank-and-percentile tables, perform
regression analysis, apply Fourier transformations to your data, and so on.
Consult Excel’s online Help for details.
Some data is available for you to experiment with statistical functions.
General form:
=AVERAGE(range) or
=AVERAGE(number1,number2,number3,…)
Cells containing text or logical values, and empty cells, are ignored but cells
containing a zero value are included.
8.2 MAX
This function returns the largest value in a given range.
General form:
=MAX(range) or
=MAX(number1,number2,number3,…)
1 Click in H38.
2 Type
8.3 COUNT
This function works out how many cells in a given range contain numbers
(including dates and formulae with numerical answers). It ignores blank
cells and cells containing text, logical or error values.
General form:
=COUNT(range) or
=COUNT(number1,number2,number3,…)
1 Click in H39.
2 Type
=count(G13:G33)
8.4 COUNTBLANK
COUNTBLANK counts the number of empty cells in a range.
General form:
=COUNTBLANK(range)
You have to be careful with this function because a cell may sometimes
appear to be blank when it isn’t really. This could be because it contains the
null string “”, a space “ “, or a formula with answer zero.
Example: Calculate the number of days when no Pink Hyssop was sold.
General form:
=SUMIF(range_to_test,criteria,range_to_sum)
where
• range_to_test is the range to be tested
• criteria stipulates what the test is
• range_to_sum is the range containing the values to be summed if the
test is satisfied
=sumif(B13:B33,”Thyme”,Sun)
8.6 COUNTIF
This function counts the number of cells in a range that match specified
criteria.
General form:
=COUNTIF(range,criteria)
Example:
Suppose you want to count how many cells in the Sales of herbs table
contain values greater than 20.
8.7 SUMPRODUCT
When given two or more sets of numbers, this function multiplies the
corresponding values together, adds up those products, and returns the
sum. Any non-numeric values are treated as though they are zeros.
=SUMPRODUCT(array1,array2,array3,…)
Example:
Look at the table in the cells B46:H53 giving dimensions of five panels that
are to be painted. The total area to be painted can be obtained by adding
up the areas (height multiplied by width) of the individual panels.
There are many other functions to help with statistical calculations. Browse
through what is offered in the Statistical category of the Insert Function
dialog box.
The Analysis Toolpak (see section 16.6) will enable you to do many things
of a statistical nature such as
• produce rank-and-percentile tables
• extract random or periodic samples from a data set
• perform regression analysis
• apply Fourier transforms to data
9 Lookup functions
If you have information stored in a list or table, there are functions to help
you.
To consider specific examples,
9.1 CHOOSE
This function chooses a value or action to perform from a list of values,
based on an index number.
=CHOOSE(index_num,value1,value2,… )
The dollars ($) are needed because the formula is going to be copied.
3 Click in C6 and drag the formula down through cells C7:C12 (use
the Fill Handle).
The results should be Autumn, Winter, Summer, Spring, Summer,
Autumn, and Winter.
1 Look at Table 2.
2 Click in C15 and enter today’s date (type it in or use the TODAY
function — see section 6.5.1).
3 Click in F15 and enter
=weekday(C15,1)
9.2 VLOOKUP
The VLOOKUP function will retrieve information from a table (which can
itself be part of a larger table). You do not have to know precisely where the
value you want is stored. The function decides which row of the table is to
be used — it finds the position, in the first column of the table, of the largest
value that is less than or equal to a lookup_value supplied by you. You tell
it which column is to be used for retrieving the information you require.
=VLOOKUP(lookup_value,table_array,col_index,range_lookup)
where
• lookup_value is the value to be found in the first column of the table
(it can be a value, reference or text string).
• table_array is the array (or range name) that defines the table.
If range_lookup is TRUE, the values in the leftmost column of the
table must be sorted in ascending order.
If range_lookup is FALSE, the leftmost column of the table does not
have to be sorted.
• col_index tells the function which column of the table to look in to find
the function’s result. This number must be greater than or equal to 1
and never greater than the number of columns in the table.
• range_lookup is a logical value that determines whether lookup_value
has to be matched exactly (set range_lookup to FALSE) or
approximately (set range_lookup to TRUE or omit it completely).
The function works with vertical tables — those where the comparison
values are in the leftmost column. These values can be either numbers or
text.
If your table is a horizontal one (with the comparison values in the first row),
use the similar function called HLOOKUP.
Example 1:
Look at Table 3 on the LookUp worksheet and find the value in Column 4
of the table corresponding to the largest value in Column 1 that is not
greater than 30.
Here Column 1 refers to the first column in the table (not to the first column
of the worksheet). Other columns in the table are counted relative to that
first column.
Because you are looking for the largest value that is not greater than 30,
the lookup_value is 30.
1 Click in C42 and type the name of one of the listed trees,
2 Click in C43 and enter a formula that will look up the tree in the table
and return the answer as to whether it is Evergreen or Deciduous.
Enter
=vlookup(C42,B34:C39,2,FALSE)
1 Click in C56 and type the name of one of the countries listed in the
table.
2 Click in C57 and enter a formula that will give the Gross Profit for the
country in C56.
3 Check the answer is correct.
4 Change the value in C56 and check that the value in C57 changes
automatically and correctly.
Note: If your comparison values and results are in separate areas of your
worksheet, consider using the function LOOKUP.
9.3 TRANSPOSE
You can change the orientation of a rectangular array of values from
vertical to horizontal (or vice versa). The first row of a horizontal array
becomes the first column of the new vertical array (and so on).
General form:
=TRANSPOSE(array)
This function must be entered as an array formula into a range that has as
many rows and columns as the original array has columns and rows.
Example:
Scroll down to Table 6 in cells B63:E75. It shows distributions of word
lengths in some letters signed Quintus Curtius Snodgrass that may have
been written by Mark Twain. The table has 13 rows and 4 columns. It could
be transposed and stored in B79:N82 (4 rows by 13 columns) as follows:
1 Select the cells B79:N82 (the location for the transposed table).
2 Type the following formula, but do not press the Enter key
3 Press the Ctrl, Shift and Enter keys together to enter the function in
each cell of the selected range.
4 Check that you have achieved the desired result.
Another way of transposing a rectangular area of cells is to:
• Copy the cells.
• Click in the cell that is to be in the top left-hand corner of the
transposed range.
• Select Edit | Paste Special.
• Tick the Transpose box and click OK.
10 Reference functions
If you wish to manipulate references, there are functions to help with that
too. This document will not consider such functions in any detail but a
couple of examples will give you an idea of the kind of thing that can be
done.
10.1 ADDRESS
This function enables you to build a reference from numbers.
General form:
=ADDRESS(row_num,col_num,abs_num,a1,sheet_name)
where
• row_num and col_num determine the row and column values
• abs_num determines whether the resulting address uses absolute
references; values are
1 absolute
2 absolute row, relative column
3 relative row, absolute column
4 relative
• a1 is a logical value;
TRUE gives a resulting address in A1 format
=OFFSET(ref,rows,cols,height,width)
where
• ref is the base reference from which the offset is calculated
• rows specifies the vertical distance from the base reference to the
new reference (positive value means below the base reference;
negative above)
• cols specifies the corresponding horizontal distance (positive to the
right, negative to the left of the base reference)
• height and width specify the shape of the resulting reference; they are
optional but must be positive if included. When they are omitted, the
resulting reference has the same dimensions as the base reference.
So, =OFFSET(B5:E10,-3,2) will return the reference D2:G7.
Now suppose that you want to calculate 3 2 + 4 2 . You could store 3 in cell
A1, and 4 in cell B1. The required formula would then be
=SQRT(POWER(A1,2)+POWER(B1,2))
12 Errors
Occasionally you may get an error value in a cell. This happens when Excel
cannot deal sensibly with the formula you have supplied. There are several
different errors caused in different ways as shown in the table below.
Error Explanation
#NAME? A text string has not been enclosed in double quotes, or, you
entered a name in a formula that is not in the Define Name
dialog box list (see section 14.3).
If you apply the Text format to a cell that contains a formula, that formula
will be thought of as text and displayed as such in the cell. If there is a
formula in another cell which refers to this text-formatted cell, the result in
that cell will be either the text value itself (for a formula that is just a direct
reference and does not involve calculations) or the error value #VALUE!.
Tip: The procedure described above can be a useful way of seeing the
effect of removing a formula without actually deleting it.
• Apply the text format to the cell containing the formula.
• Click the formula bar and press Enter (to recalculate the worksheet
and display the text value).
• Look for any resulting #VALUE! errors.
To restore the formula
• Apply a numeric format to the cell.
• Click the formula bar and press Enter.
Note: You can use the Edit | Find command to locate error values.
=ISERR(value)
=ISERROR(value)
=ISNA(value)
=IF(ISERROR(C2/C3),0,C2/C3)
4 In E8 enter
=C11+567
5 In F10 enter
=2*E8
6 From the Tools menu select Formula Auditing, and then Show
Formula Auditing Toolbar.
7 Click in F10 (the latest cell containing an error).
8 Click the Trace Error button on the Formula Auditing toolbar
(seventh button from the left).
The resulting display is shown below. Red arrows trace the error chain and
blue arrows show the cells used in the first of the formulae to give an error.
3 Press Enter.
You should get the answer 306.
You will have noticed that the table containing the original data is
surrounded by blank rows and columns. When using natural language
formulae, leave blank rows and columns between the table and any cells
containing calculations involving natural language references.
If you would like to see the effect of entering a value in one of the blank
regions:
5 Use the Undo button to get back to the stage where D12 was blank.
It is also possible to define labels and have Excel automatically substitute
them for cell references.
This will give the same result as entering the formula =D19+E19 (£1.20).
Important: When you define a name for a cell, the name of the worksheet
is part of that definition and the cell reference is absolute. So, the actual
definition of the name for D19 is NamedRefs!$D$19.
3 Click OK.
4 Click on the down-arrow in the Name box at the left-hand end of the
formula bar and check that this new name is included.
To use this named range (Number_of_tins),
1 Click in C31.
2 Type
=sum(Number_of_tins)
and press Enter.
The result should be 215.
So that you can use it later,
1 Click in F25.
6 Press the F3 key (a quick way to get to the Paste Name dialog box).
7 Select Cost_of_tin and click OK.
8 Your formula is now complete so press the Enter key (£125.16).
9 Copy that formula down through cells F26:F28.
10 Click in F27 and check that the formula is correct.
Note: If you need to select more than one name in that Apply Names
dialog box, just hold down the Shift key as you select the names.
1 Click in C38.
2 Select Insert | Name | Paste.
3 In the Paste Name dialog box, click on the Paste List button.
15.1 Examples
1 Click on the Arrays tab of the workbook.
The red and blue numbers can be thought of as arrays.
Matrix multiplication
Array multiplication 2 9
8 4
Matrix multiplication 14 6
20 10
16 Miscellaneous
16.1 Recalculation
Manual recalculation
If you have a large workbook involving lots of formulae, and you change the
value in a cell, Excel’s automatic recalculations can take a while to
complete. To save time, you may like to consider changing to manual
recalculation. Then Excel will not recalculate until you tell it to.
=SUM(Sheet2:Sheet5!H2)
=RC[-2]+RC[-1]
means take the contents of the cell in the same row but two columns to the
left and add the contents of the cell in the same row but one column to the
left.
A relative reference must include square brackets around the number in the
reference.
[+n] [-n]
R n rows below the formula cell n rows above the formula cell
R1C1 references were not used in this document but there may be
occasions when you will find this approach useful (perhaps when writing
macros). If you are interested in trying R1C1 references,
17 Analysis Toolpack
Excel provides an Analysis Toolpak add-in — a set of data analysis tools
for use in statistical and engineering analyses.
18 What-if analysis
One of the most useful benefits when using a spreadsheet is the ability to
carry out what-if analyses. When Excel is configured to recalculate