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

PRATYUSHA HOME

Hi interesting readers, Prathysha NGO, Chennai. Bala gurukul orphange, chennai. Pratyusha support, Hyderbad. Krr oldage home, Vizag. Disa foundation.......................................................... so on..................... Join hands with us for a good cause. Thank you all. Contact  us on our  whatsapp no:  8939374254    for the more details. You can pay through Paytm scanner. for each donor we will give an online PDF book as a gift. Thank you Pratyusha Home
Java Contents  -This free course is for those wishing to learn the Java programming language. It is aimed at complete beginners to the subject, and no knowledge is assumed. The software you need to do the course is set out below, in the "What you need to get started" link. Once you have downloaded everything you need, you can make start by following the links on this page. Good luck! Home Page > Section 1: Getting Started with Java 1. What you need to get started 2. The Netbeans Software 3. Java Comments 4. The Structure of Java Code 5. How to Run your Java Programmes 6. Printing to the Output Window 7. Sharing your programmes with others > Section 2: Variables 1. Integer Variables in Java 2. Double Variables 3. Short and Float 4. Operator_Precedence 5. Java String Variables 6. Getting Input from the User 7. Option Panes > Section 3: Control Flow 1. If Statements in Java 2. If Else Statements 3...
Microsoft Word  -This is a full and free computer course for Microsoft Word 2007 to 2016. It is designed for complete beginners. If you're not sure whether or not you're in the right place take a look at Section 1.1 below, How to Open Microsoft Word. For those who have older versions of Word (versions up to 2003) then your tutorials are here:  Word 2000 to 2003 (Older versions) Home Page > Section One - Get Started with Microsoft Word   1.1 How to Open Microsoft Word 1.2 Finding Your Way Around 1.3 Typing Your First Letter 1.4 How to Save Your Work 1.5 Highlighting Techniques 1.6 Opening Word Documents 1.7 Add an Address To A Letter 1.8 Spelling and Grammar Checking > Section Two - Document Formatting   2.1 Crime Statistics Document 2.2 Bold Text 2.3 Italics and Underline 2.4 Seting Tab Stop Positions 2.5 Creating Bulleted Lists 2.6 Working With Fonts 2.7 Setting Up Page Margins > Section Three - C...