The 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:
(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
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: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
=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: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 Conditonal Formatting. This is about colouring cells depending on their values. Looks nice on a spreadsheet!
Buy Complete M.S. Excel Tutorial Book From Amazon : https://amzn.to/2yEYXgk
0 Comments
Please share your opinions and suggestions with us.