Skip to main content

The Excel IF Function

 - The IF function can be quite useful in a spreadsheet. It is used when you want to test for more than one value. For example, has a bill been paid or not? If it has, you can deduct the amount from the money you have left to spend; if it hasn't, keep it on your debt list. Later, you'll see how to use the IF Function to grade student exam scores. If the student has above 80, award an A grade; if the student has below 30, award a fail grade. First, here's what an IF Function looks like:
IF(logical_test, value_if_true, value_if_false,)
The thing to note here is the three items between the round brackets of the word IF. These are the arguments that the IF function needs. Here's what they mean:
logical_test
The first argument is what you want to test for. Is the number in the cell greater than 80, for example?
value_if_true
This is what you want to do if the answer to the first argument is YES. (Award an A grade, for example)
value_if_false
This is what you want to do if the answer to the first argument is NO. (Award a FAIL grade.)
If that's not terribly clear, an example may clear things up. Open a new spreadsheet, and do the following:
  • Widen the B column a bit, as we'll be putting a message in cell B1
  • Now click in cell A1 and type the number 6
  • Type the following in the formula bar (The right angle bracket after A1 means "Greater Than".)
=IF(A1 > 5, "Greater than Five", "Less than Five")
Hit the enter key on your keyboard and your spreadsheet should look like ours below:
An Excel 2007 IF Statement
(Make sure you have all the commas and double quotes in the correct place, otherwise Excel will give you an error message. That right angle bracket ( > ) is known as a Conditional Operator. You'll meet some others shortly.)
But what we're saying in the IF function is this:
logical_test: Is the value in cell A1 greater than 5?
value_if_true: If the answer is Yes, display the text "Greater than Five"
value_if_false: If the answer is NO, display the text "Less than Five"
So your first tell Excel what you want to check the cell for, then what you want to do if the answer is YES, and finally what you want to do if the answer is NO. You separate each part with a comma.
Exercise
Try this:
  • Click into cell A1
  • Change the 6 into a 4
  • Hit the enter key on your keyboard
What happens?

Exercise
Now type the number 5 in cell A1. What happens now?
For the last exercise above, Excel should tell you that 5 is "Less than 5"! It does this because the answer to your logical test was NO. We were testing if the number in cell A1 was greater than 5. Since 5 is not greater than 5, the answer to the question is NO. We've told Excel to display a message of "Less than 5", if the answer was NO. In other words, we didn't tell Excel what to do if the value in cell A1 was the same as 5.
The solution to this is to use a different Conditional Operator. We used the Greater Than ( > ) operator. Here's some more:
<      Less Than
>=    Greater than Or Equal To
<=    Less than Or Equal To
<>    Not Equal To
For the second and third operators above, you type an angle bracket followed by the equals sign. There are no spaces between the two. For the final one, it's a left angle bracket followed by a right angle bracket.
So for our exercise, the symbol we should have used was the one for Greater than Or Equal To. Change your IF function to this and try again:
=IF(A1 >= 5, "Greater than or Equal to Five", "Less than Five")

Exercise
Test the A1 cell to see if the value is less than or equal to 5. If it is, display a suitable message. If it's not, display the message "Greater than Five".

Complex If Functions

The If Functions you've just met are consider fairly simple ones. They can get really complex!
Consider our Student Exam problem. The spreadsheet we created to track our students looks like this, from an earlier section:
However, we want to display the following grades as well:
A If the student scores 80 or above
B If the student scores 60 to 79
C If the student scores 45 to 59
D If the student scores 30 to 44
FAIL If the student scores below 30
With such a lot to check for, what will the IF Function look like? Here's one that works:
=IF(B2>=80, "A", IF(B2>=60, "B", IF(B2>=45, "C", IF(B2 >=30, "D", "Fail" ) ) ) )
Quite long, isn't it? Look at the colours of the round brackets above, and see if you can match them up. What we're doing here is adding more IF Functions if the answer to the first question is NO. If it's YES, it will just display an "A".
But take a look at our Student Exam spreadsheet now:
After the correct answer is displayed in cell B14 on the spreadsheet above, we used AutoFill for the rest!
Don't worry if that long IF statement is making your brain hurt - it is quite complicated.

In the next part, we'll take a look at using And and OR with IF Statements.

Comments

Popular posts from this blog

Beginners PHP  -This is a complete and free PHP programming course for beginners. It's assumed that you already have some HTML skills. But you don't need to be a guru, by any means. If you need a refresher on HTML, then click the link for the Web Design course on the left of this page. Everything you need to get started with this PHP course is set out in section one below. Good luck! Home Page > PHP Section One - An Introduction to PHP 1. What is PHP and Why do I need it? 2. What you need to get started 3. Installing and testing Wampserver 4. Troubleshooting > PHP Two - Getting Started With Variables 1. What is a Variable? 2. Putting text into variables 3. Variables - some practice 4. More variable practice 5. Joining direct text and variable data 6. Adding up in PHP 7. Subtraction 8. Multiplication 9. Division 10. Floating point numbers > PHP Three - Conditional Logic 1. If Statements 2. Using If Statements 3....
Visual Basic .NET Contents Page   -This computer course is an introduction to Visual Basic.NET programming for beginners. This course assumes that you have no programming experience whatsoever. It's a lot easier than you think, and can be a very rewarding hobby! You don't need to buy any software for this course! You can use the new FREE Visual Basic Express Edition from Microsoft. To see which version you need, click below: Getting the free Visual Studio Express - Which version do I need? > VB .NET One - Getting Started   1. Getting started with VB.NET 2. Visual Basic .NET Forms 3. Adding Controls using the Toolbox Home Page 4. Adding a Textbox to the Form 5. Visual Basic .NET and Properties 6. The Text Property 7. Adding a splash of colour 8. Saving your work 9. Create a New Project >   VB .NET Two - Write your first .NET code   1. What is a Variable? 2. Add a coding button to the Form 3. Writing y...
The Excel SumIF Function  - Another useful Excel function is SumIF. This function is like CountIf, except it adds one more argument: SUMIF( range ,  criteria ,  sum_range ) Range and criteria are the same as with  CountIF  - the range of cells to search, and what you want Excel to look for. The Sum_Range is like range, but it searches a new range of cells. To clarify all that, here's what we'll use SumIF for. (Start a new spreadsheet for this.) Five people have ordered goods from us. Some have paid us, but some haven't. The five people are Elisa, Kelly, Steven, Euan, and Holly. We'll use SumIF to calculate how much in total has been paid to us, and how much is still owed. So in Column A, enter the names: In Column B enter how much each person owes: In Column C, enter TRUE or FALSE values. TRUE means they have paid up, and FALSE means they haven't: Add two more labels: Total Paid, and Still Owed. Your spreadsheet should look something li...