Skip to main content

Using AND and OR in Excel IF Statements

 - (You can skip this part, if you found the IF Statements in the previous tutorial a touch too tricky! Just move on to Conditional Formatting via the link at the bottom of the page.)

The Excel AND Function

The Excel function AND takes a least two values and tells you whether or not they are true. As a simple example, take a sequence like this:
A sequence of values on a spreadsheet
You can use AND to see if this sequence is all "Won". Enter the following formula in cell E1:
=AND(B1="Won", C1="Won", D1="Won")
When you press the enter key on your keyboard, cell E1 will have a value of False. It's False because the AND function is checking each cell for a value of "Won". If just one of these values is not "Won" then the whole AND function returns false. If you change the value in cell C1 to Won and press the enter key, cell E1 will have a value of True.
With the AND function, each test for true or false needs to be separated with a comma.

The Excel OR Function

Similar to AND, but if just one of your tests is true then Excel return true for the whole OR function.

Using AND/OR in IF Statements

AND and OR are more useful when used with IF Statements. Try this. Create the simple spreadsheet below:
A student with A values in cells B2 and C2
We first want to test if a student got straight As. If so, we'll display a suitable message in the D column.
Click inside cell D2. Enter the following formula:
=IF(AND(B2="A", C2="A"), "Straight A Student!", "Not a straight A student")
Now press the enter key on your keyboard. You see that cell D2 contains the text "Straight A Student!". Your spreadsheet should look like this:
Using the AND function witht he IF function in Excel
Now click inside cell B2. Change the grade from A to B. Press the enter key again and cell D2 will change to "Not a straight A student":
AND used with IF in Excel
So how does it work? The first part of the IF Statement uses the AND function:
AND(B2="A", C2="A")
This just checks cells B2 and C2 for the text "A". (If you're checking for text values, you have to surround the text with double quotes.) If both of these are true, if cells B2 and C2 do indeed contains the text "A", then the whole of the AND part evaluates to TRUE. And if the first part of an IF Statement is TRUE then Excel uses whatever you have after the first comma. For us, this was just some text, "Straight A Student". If the AND part is FALSE, then Excel uses whatever you have after the second comma. After our second comma, we have the text "Not a straight A student". Excel is doing this:
If AND(B2="A", C2="A") evaluates to TRUE Then
Display "Straight A Student"
If AND(B2="A", C2="A") evaluates to FALSE Then
Display "Not a straight A Student"
Let's try the OR function. Click in to cell B3. Enter a capital letter F. In cell C3 enter a capital letter A. In cell D3, add the following formula:
=IF(OR(B3="F", C3="F"), "At least one F", "Not a single F!")
Your spreadsheet should look like this:
Using the OR function with the IF  function in Excel
This time, we've used OR as the test for our IF Statement. Now, only one of the conditions needs to be TRUE for the whole OR statement to evaluate as TRUE. If the OR statement is TRUE then the part after the first comma gets executed by Excel. This was to display the test "At least one F". If neither cell B3 or cell C3 contains a letter F then the part after the second comma gets executed. This was the text "Not a single F!".
Try it out. Change the F to a B, for example. You should see the text in cell D3 change.

In the next lesson, we'll explore Conditional Formatting. This is about colouring cells depending on their values. Looks nice on a spreadsheet!

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...