Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

COMP1022Q

Introduction to Computing with Excel VBA

Cell Formula Basics

David Rossiter, Oz Lam and Gibson Lam


Outcomes
• After completing this presentation, you are
expected to be able to:
1. Understand the basics of using cell formulas
2. Write cell formulas using appropriate
operations and functions
3. Describe the expected results of some cell
formulas

COMP1022Q Cell Formula Basics Page 2


In This Presentation
• We will look at the following topics in this
presentation:
– The basics of cell formulas
– Arithmetic operators
– Relational operators
– Logic functions
– String concatenation
– String handling

COMP1022Q Cell Formula Basics Page 3


Cell Formulas
• Every cell formula starts with a =
• For example, this is a simple formula
calculating the average cost of meals:
= SUM( B13:B17 ) / 5

• SUM is a
function • A range of • The total number
cells which of meals
contains the
costs of 5 • / is an operator
meals
Arithmetic Operators 1/2
• In Excel, you can do basic calculations
with arithmetic operators, like those
you learned in school:
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power

COMP1022Q Cell Formula Basics Page 5


Arithmetic Operators 2/2
• For example, if you want to add two
numbers 3 and 10, you can use the
+ operator like this:
= 3 + 10
• There are only 5 arithmetic operators but there are
many other functions in Excel helping you
perform other clever calculations
• For example, you can use
=AVERAGE(B13:B17) to do the same thing as
the formula shown on the slide before
• These examples show the use
Examples Of of all 5 arithmetic operators in
Arithmetic Operators Excel cell formulas

This cell has the name A


This cell has the name B

The formula is
shown as text by
adding ' in
front of the
formula:
'= A + B
Some Commonly Used Number Functions
• There are many functions that can be used in cell formulas
• Here we look at 7 common functions for handling numbers:
SUM for finding the total of a group of numbers
AVERAGE for finding the average of a group of numbers
STDEV for finding the standard deviation of a group of numbers
MAX for finding the maximum in a group of numbers
MIN for finding the minimum in a group of numbers
RANK for ranking a number in a group of numbers
COUNTIF for counting the number of occurrences of a value
• On the next slide, we show an example which uses these

COMP1022Q Cell Formula Basics Page 8


Example of Some Commonly Used Number Functions
Here are the Formulas Used
Relational Operators
• Sometimes you need to know about the relationship
between two things
• For example, you might need to compare whether
two values are equal, or if they are different
• Relational operators help you achieve that
• The operators include:
= equal to <> not equal to
< smaller than <= smaller than or equal to
> larger than >= larger than or equal to

COMP1022Q Cell Formula Basics Page 11


Results of Relational Operators
• The result of a relational operator is always
TRUE or FALSE
• Note that they are shown using capital letters in Excel
• For example, to test if the value in cell A4 is
larger than that in cell B6, we can write
=A4 > B6
• The result won’t be a number; it will be either
TRUE or FALSE

COMP1022Q Cell Formula Basics Page 12


Examples Of • These examples show the use
of all 6 relational operators
Relational Operators

This cell has the name A


This cell has the name B
Using Relational Operators in COUNTIF
• We learned that the COUNTIF function helps us
count the number of occurrences of some value,
e.g. COUNTIF(J6:J16, "A")
– "A" is called the criterion of
the COUNTIF function
• Apart from a value, we can also add a relational
operator in the criterion
• In the next slide, we show an example using the
student marks file we have seen before
COMP1022Q Cell Formula Basics Page 14
Example of • This example counts the
number of students who
Using COUNTIF with failed the final exam, with
a Relational Operator a pass mark of 40

"<40" means
any value
smaller than 40
Using Logic
• There are other functions which do comparisons,
called logic functions
• In real life, we usually need to make a decision
based on several criteria (which are usually called
conditions in Computing)
• For example, you decide what you will eat for
lunch based on the price and the quality of the food
and how far away the restaurant is
• We can use logic functions to combine the results
of conditions together to make a final decision

COMP1022Q Cell Formula Basics Page 16


Some Commonly Used Logic Functions
• Here we look at 3 commonly used logic functions
• All logic functions return either TRUE or FALSE:
returns TRUE if all the input(s) are TRUE, Usually
AND
otherwise it returns FALSE these have
at least
returns TRUE if at least one of the inputs two inputs
OR
is TRUE, otherwise it returns FALSE
This has just
returns TRUE if the input is FALSE,
NOT one input.
otherwise it returns FALSE The result is
the opposite
• On the next slide we show of the input.
examples of these being used

COMP1022Q Cell Formula Basics Page 17


Examples of Some Commonly Used Logic Functions
The Formulas Used

Ctrl `
String Concatenation
• In computer language, ‘string’ means ‘a piece of text’
• Concatenate means putting one string at the end of
another string
• In Excel cell formulas, you can concatenate two
strings by using the & operator or the CONCATENATE
function
• For example, this formula:
="Happy" & "Birthday"
and this formula:
=CONCATENATE("Happy", "Birthday")
both produce the same result: HappyBirthday
String Concatenation
• Here is an example of both:

This cell has the name A


This cell has the name B

COMP1022Q Cell Formula Basics Page 21


Some Commonly Used String Handling Functions
• There are many other functions for strings
• Here we look at some:
LEN Count the number of characters in a string
UPPER Convert all letters into upper case letters i.e. ABC
LOWER Convert all letters into lower case letters i.e. abc
LEFT Get the first few characters in a string
RIGHT Get the last few characters in a string
SUBSTITUTE Substitute (replace) some text in a string
• There are examples of these on the next slide

COMP1022Q Cell Formula Basics Page 22


Example of Some Commonly Used
String Handling Functions

This cell has the name A


This cell has the name Old
This cell has the name New
Operator Precedence
• If you enter the following formula
=5+2*3
you may think that the answer is 21
• However, Excel actually gives you the answer of 11
• That is because Excel thinks that multiplication is
more important than addition
• Therefore 2*3 is performed first, before the addition
• In computer programming, we say multiplication has
a higher precedence than addition

COMP1022Q Cell Formula Basics Page 24


Levels of Precedence in Excel
• The following table shows you the different levels of
precedence of some operators, from high to low:
^ Power
* and / Multiplication and division
+ and - Addition and subtraction
& String concatenation
= <> < <= > >= Comparison operators

• If you want to make sure one part of a formula is


evaluated first, you can use brackets. Examples:
=5+2*3 gives an answer of 11
=(5+2)*3 gives an answer of 21

You might also like