How to use the OR function
What is the OR function?
The OR function allows you to carry out a logical test in each argument and if at least one argument returns TRUE the OR function returns TRUE.
If all arguments return FALSE the OR function also returns FALSE.
Table of Contents
1. Introduction
What is a logical test?
A logical test in Excel is a test or condition that results in either a TRUE or FALSE outcome. Logical tests are essential for conditional formatting, data validation, formulas, filters, and more. Mastering logical tests unlocks a lot of functionality in Excel. Comparison operators let you build logical tests in Excel formulas.
Here is an example of a logical test:
A1<B1
A1 and B1 are relative cell references. < is a comparison operator that allows you to check if a value is smaller than another value.
If A1 contains 5 and B1 contains 4 the logical expression becomes 5<4 which returns the Boolean value FALSE because 5 is NOT smaller than 4.
What are comparison operators?
Comparison operators are the equal sign, larger than, and the smaller than signs.
- = equal sign
- > larger than
- < smaller than
You can combine comparison operators to create more types of logical tests.
- <= larger than or equal to
- >=smaller than or equal to
- <> not equal to
Here is an example that carries out two logical tests, one in each argument.
=OR(5<4, 4<5)
The first argument contains 5<4 and that returns FALSE, the second argument is 4<5 which returns TRUE.
=OR(FALSE,TRUE)
The OR function performs OR logic between the arguments and returns TRUE.
What is OR logic?
OR logic is a boolean logic function that results in TRUE if any of the inputs are TRUE. It returns FALSE if all inputs are FALSE.
OR(TRUE, TRUE) = TRUE
OR(TRUE, FALSE) = TRUE
OR(FALSE, TRUE) = TRUE
OR(FALSE, FALSE) = FALSE
What is TRUE and FALSE?
TRUE and FALSE are Boolean values, logical test return a Boolean value or their numerical equivalents.
What are the numerical equivalents to TRUE and FALSE?
The numerical equivalents to TRUE and FALSE are 1 and 0 (zero) respectively.
2. Syntax
OR(logical1, [logical2])
The or function is a logical function, it allows you to evaluate multiple conditions.
It can be used instead of nested IF functions. The OR function is even more versatile if you combine it with the AND function, there is an example below using both the OR and AND functions.
3. Arguments
logical1 | Required. A logical expression that returns a logical value or a number. |
[logical2] | Optional. You can have up to 254 additional arguments. |
4. Example
Formula in cell D3:
The picture above shows the formula in column D, it returns TRUE in cell D3, D4 and D5 because the amount is below 10.
5. Boolean arguments
The OR function arguments can result in TRUE or FALSE, however, it also treats all numbers, both positive and negative, as TRUE.
The exception to that is 0 (zero) which is treated the same as FALSE.
Formula in cell B3:
Formula in cell B3 returns TRUE. TRUE exists at least once.
Formula in cell B4:
Formula in cell B4 returns TRUE.
Formula in cell B5:
Formula in cell B4 returns FALSE. All arguments are FALSe, OR function returns FALSE.
Formula in cell B6:
The numerical equivalent to TRUE is any number other than 0 (zero). 1 is therefore TRUE. Formula in cell B6 returns TRUE.
Formula in cell C3:
The same goes for negative values. -1 is TRUE.
Formula in cell C3:
23, 55 and -34 are all TRUE.
Formula in cell C3:
0 (zero) is FALSE. All arguments are FALSE, OR function returns FALSE.
Formula in cell C3:
All numbers are not zero, they all evaluate to TRUE.
6. Multiple conditions
The array formula in cell C3 performs two logical tests. If the value in cell B3 is equal to cell E3 or E4 the formula returns TRUE.
Formula in cell C3:
The technique used in column C that compares a value to multiple values using a cell range instead of declaring each logical expression in an argument saves you time and effort. The downside is that you need to enter the formula as an array formula unless you use Excel 365.
To enter the formula above as an array formula, type the formula in a cell. Press and hold CTRL + SHIFT keys simultaneously, then press Enter once. Release all keys.
The formula is now enclosed with curly brackets, they indicate you successfully entered the formula as an array formula. Don't enter the curly brackets yourself.
6.1 Explaining formula in cell C3
Step 1 - Reference conditions
You will be using the same conditions in cell C3 and cells below, it is a good idea to create absolute references meaning they won't change when you copy the cell and paste to cells below.
The dollar sign $ lets you lock both column and row references, see reference below.
$E$3:$E$4
returns {"Binder";"Printer"}. The curly brackets tell you that this is an array of values, they can have a comma or semicolon delimiting character or both.
Step 2 - Compare conditions with cell value
The equal sign is a logical operator that lets you compare value to value or in this case value to values. It returns a boolean value TRUE or FALSE.
$E$3:$E$4=B3
becomes
{"Binder";"Printer"}="Stapler"
and returns {FALSE; FALSE}. None of the conditions match the value in cell B3.
Step 3 - Evaluate OR function
The OR function returns TRUE if at least one of the boolean values in the array is TRUE.
OR($E$3:$E$4=B3)
becomes
OR({FALSE; FALSE})
and returns FALSE.
7. Function not working
The OR function returns
- #VALUE! error if the logical expression is invalid.
- #NAME? error if you misspell the function name.
- propagates errors, meaning that if the input contains an error (e.g., #VALUE!, #REF!, #DIV/0!), the function will return the same error.
7.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
7.2 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference C4 converted to hard-coded value using the F9 key. The OR function requires non-error values which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
7.3 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
8. Array
The array formula in cell D3 evaluates two logical expressions. If the value in cell B3 is equal to any of the conditions in cells B9:B11 and the value in cell C2 is equal to any of the numbers in cells C9:C10 the formula returns TRUE.
Formula in cell D3:
Cell B3 is equal to a condition in cell B9 and cell C3 is equal to C10, the formula returns TRUE in cell D3.
8.1 Explaining formula in cell C3
Step 1 - Compare values
The equal sign is a logical operator that lets you compare value to value or in this case value to values. It returns a boolean value TRUE or FALSE.
$B$9:$B$11=B3
becomes
{"Stapler";"Calculator";"Ballpoint pen"}="Stapler"
and returns
{TRUE; FALSE; FALSE}.
Step 2 - Check if any of the conditions match cell B3
The OR function returns TRUE if at least one of the boolean values in the array is TRUE.
OR($B$9:$B$11=B3)
becomes
OR({TRUE; FALSE; FALSE})
and returns TRUE.
Step 3 - Check next cell C3
OR($C$9:$C$10=C3)
becomes
OR({19;2}=2)
becomes
OR({FALSE; TRUE})
and returns TRUE.
Step 4 - Check that all conditions evaluate to TRUE
The AND function returns TRUE if all arguments evaluate to TRUE.
AND(OR($B$9:$B$11=B3),OR($C$9:$C$10=C3))
becomes
AND(TRUE, TRUE)
and returns TRUE.
9. COUNTIF
The array formula in cell D3 evaluates two logical expressions. If the value in cell B3 is equal to any of the conditions in cells B9:B11 or the value in cell C3 is equal to any of the numbers in cells C9:C11 the formula returns TRUE.
Formula in cell D3:
Cell B3 is equal to a condition in cell B9 and cell C3 is not equal to any of the conditions in cells C9:C11, the formula returns TRUE in cell D3.
9.1 Explaining formula in cell C3
Step 1 - Count values based on first conditions
The COUNTIF function counts the number of cells that meet the condition.
COUNTIF(range, criteria)
COUNTIF(B3,$B$9:$B$11)
becomes
COUNTIF("Stapler", {"Stapler"; "Calculator"; "Ballpoint pen"})
and returns {1; 0; 0}.
Step 2 - Count values based on second conditions
COUNTIF(C3,$C$9:$C$11)
becomes
COUNTIF(2, {19; 3; 8})
and returns {0; 0; 0}.
Step 3 - Count values
The OR function returns TRUE if at least one value is TRUE.
OR(COUNTIF(B3,$B$9:$B$11), COUNTIF(C3,$C$9:$C$11))
becomes
OR({1; 0; 0}, {0; 0; 0})
and returns TRUE. 1 is the numerical equivalent to TRUE.
10. IF
Formula in cell D3:
10.1 Explaining formula in cell C3
Step 1 - Check the first condition
The larger than character is a logical operator, it checks if a value is larger than another value. It returns boolean value TRUE or FALSE.
C3>5
becomes
11>5
and returns TRUE.
Step 2 - Check the second condition
The smaller than character is a logical operator, it checks if a value is smaller than another value. It returns boolean value TRUE or FALSE.
C3<1
becomes
11<1
and returns FALSE.
Step 3 - Evaluate OR function
OR(C3>5,C3<1)
becomes
OR(TRUE, FALSE)
and returns TRUE.
Step 4 - Evaluate IF function
The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.
IF(logical_test, [value_if_true], [value_if_false])
IF(OR(C3>5,C3<1),B3,"")
11. Contains text
Formula in cell D3:
11.1 Explaining formula in cell C3
Step 1 - Search value for strings
The SEARCH function returns the position of the character at which a specific text string is found. Luckily the IF function accepts any number as TRUE except 0 (zero), however, the SEARCH function returns an error #VALUE! if it can't find the text string.
SEARCH($E$3:$E$4,B3)
becomes
SEARCH({"pen";"ap"},"Stapler")
and returns {#VALUE!; 3}.
Step 2 - Check if number
To avoid the error value I use the ISNUMBER function that returns TRUE if a number and FALSE if anything else, also formula errors.
ISNUMBER(SEARCH($E$3:$E$4,B3))
becomes
ISNUMBER({#VALUE!; 3})
and returns {FALSE; TRUE}
Step 3 - Evaluate OR function
OR(ISNUMBER(SEARCH($E$3:$E$4,B3)))
becomes
OR({FALSE; TRUE})
and returns TRUE.
Step 3 - Evaluate IF function
The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.
IF(logical_test, [value_if_true], [value_if_false])
IF(OR(ISNUMBER(SEARCH($E$3:$E$4,B3))),B3,"")
becomes
IF(TRUE,B3,"")
and returns "Stapler".
12. Conditional Formatting
The image above shows Conditional Formatting highlgihting cells in ceolumn B if they match the conditions specified in column D.
Conditional formatting formula applied to cell range B3:B8:
12.1 Explaining conditional formatting formula
Step 1 - Define reference to conditions
This reference must stay unchanged or locked to D3:D4, use the dollar sign character to lock a reference for both column and row.
$D$3:$D$4
Step 2 - Compare values
The equal sign is a logical operator that lets you compare value to value or in this case value to values. It returns a boolean value TRUE or FALSE.
B3=$D$3:$D$4
becomes
"Stapler"={"Printer"; "Ruler"}
and returns {FALSE; FALSE}
Step 3 - Evaluate logical tests
OR(B3=$D$3:$D$4)
becomes
OR({FALSE; FALSE})
and returns FALSE.
12.2 How to apply Conditional formatting
How to apply Conditional Formatting
13. How to filter using OR logic between columns - Autofilter and Excel tables
The filter feature in Excel won't allow you to do OR logic between columns, however, you can if you allow assistance from a formula.
I want to filter all rows where Category is equal to 1 OR Size is equal to A, the formula in E3 is:
Alternative formula:
Copy the formula and paste to cells below.
Now it is time to apply a filter to your data set. Go to tab "Data" on the ribbon, press with left mouse button on "Filter" button or press CTRL + SHIFT + L.
Arrows appear on your headers, see picture below.
Press with mouse on the arrow next to "Formula" header and deselect 0 (zero). If you use the alternative formula, simply deselect False.
Press with left mouse button on "OK" button. Here is the filtered table.
To remove the filter simply press with left mouse button on the "Clear" button on the ribbon.
14. Get Excel *.xlsx file
'OR' function examples
This post explains how to lookup a value and return multiple values. No array formula required.
Table of Contents Automate net asset value (NAV) calculation on your stock portfolio Calculate your stock portfolio performance with Net […]
Table of Contents Excel monthly calendar - VBA Calendar Drop down lists Headers Calculating dates (formula) Conditional formatting Today Dates […]
Functions in 'Logical' category
The OR function function is one of 16 functions in the 'Logical' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form