Thursday, 12 May 2016

Nov 26 Using IF function in Excel: formulas for numbers, text, dates, blank cells

Excel IF function - syntax and usage

The IF function is one of Excel's logical functions that evaluates a certain condition and returns the value you specify if the condition is TRUE, and another value if the condition is FALSE.
The syntax for Excel IF is as follows:
IF(logical_test, [value_if_true], [value_if_false])
As you see, the IF function has 3 arguments, but only the first one is obligatory, the other two are optional.
  • logical_test - a value or logical expression that can be either TRUE or FALSE. Required.In this argument, you can specify a text value, date, number, or any comparison operator. For example, your logical test can be expressed as or B1="sold", B1<12/1/2014, B1=10 or B1>10.
  • value_if_true - the value to return when the logical test evaluates to TRUE, i.e. if the condition is met. Optional.For example, the following formula will return the text "Good" if a value in cell B1 is greater than 10: =IF(B1>10, "Good")
  • value_if_false - the value to be returned if the logical test evaluates to FALSE, i.e. if the condition is not met. Optional.For example, if you add "Bad" as the third parameter to the above formula, it will return the text "Good" if a value in cell B1 is greater than 10, otherwise, it will return "Bad": =IF(B1>10, "Good", "Bad")
Excel IF function - formula example

Excel IF function - things to remember!

Though the last two parameters of the IF function are optional, your formula may produce unexpected results if you don't know the underlying logic beneath the hood.
1. If value_if_true is omitted.
If the value_if_true argument is omitted in your Excel IF formula (i.e. there is only a comma following logical_test), the IF function returns zero (0) when the condition is met. Here is an example of such a formula: =IF(B1>10,, "Bad")
If you don't want your IF formula to display any value when the condition is met, enter double quotes ("") in the second parameter, like this: =IF(B1>10, "", "Bad") Technically, in this case the formula returns an empty string, which is invisible to the user but perceivable to other Excel functions.
The following screenshot demonstrates the above approaches in action, and the second one seems to be more sensible:
IF formulas with the value_if_true argument omitted
2. If value_if_false is omitted.
If you don't care what happens if the specified condition is not met, you can omit the 3rd parameter in your Excel IF formulas, which will result in the following.
If the logical test evaluates to FALSE and the value_if_false parameter is omitted (there is just a closing bracket after the value_if_true argument), the IF function returns the logical value FALSE. It's a bit unexpected, isn't it? Here is an example of such a formula: =IF(B1>10, "Good")
If you put a comma after the value_if_true argument, your IF function will returns 0, which doesn't make much sense either: =IF(B1>10, "Good",)
And again, the most reasonable approach is to put "" in the third argument, in this case you will have empty cells when the condition is not met: =IF(B1>10, "Good", "")
IF formulas with the value_if_false argument omitted
3. Get the IF function to display logical values TRUE or FALSE
If you want your Excel IF formula to display the logical values TRUE and FALSE when the specified condition is met and not met, respectively, type TRUE in the value_if_true argument. The value_if_false parameter can be FALSE or omitted. Here's a formula example:
=IF(B1>10, TRUE, FALSE)
or
=IF(B1>10, TRUE)
an example of the IF function that displays logical values TRUE or FALSE
Note. If you want your IF formula to return TRUE and FALSE as the logical values (Boolean) that other Excel formulas can recognize, make sure you don't enclose them in double quotes. A visual indication of a Boolean is middle align in a cell, as you see in the screenshot above.
If you want to "TRUE" and "FALSE" to be usual text values, enclose them in "double quotes". In this case, the returned values will be aligned left and formatted as General. No Excel formula will recognize such "TRUE" and "FALSE" text as logical values.
4. Get IF to perform a math operation and return a result
Instead of returning certain values, you can make your IF formula to test the specified condition, perform a corresponding math operation and return a value based on the result. You do this by using arithmetic operators or other Excel functions in the value_if_true and /or value_if_false arguments. Here are just a couple of formula examples:
Example 1: =IF(A1>B1, C3*10, C3*5)
The formula compares the values in cells A1 and B1, and if A1 is greater than B1, it multiplies the value in cell C3 by 10, by 5 otherwise.
Example 2: =IF(A1<>B1, SUM(A1:D1), "")
The formula compares the values in cells A1 and B1, and if A1 is not equal to B1, the formula returns the sum of values in cells A1:D1, an empty string otherwise.

Using the IF function in Excel - formula examples

Now that you are familiar with the Excel IF function's syntax, let's look at some formula examples and learn how to use IF as a worksheet function in Excel.

IF function examples for numbers: greater than, less than, equal to

The use of the IF function with numeric values is based on using different comparison operators to express your conditions. You will find the full list of logical operators illustrated with formula examples in the table below.
ConditionOperatorFormula ExampleDescription
Greater than>=IF(A2>5, "OK",)If the number in cell A2 is greater than 5, the formula returns "OK"; otherwise 0 is returned.
Less than<=IF(A2<5, "OK", "")If the number in cell A2 is less than 5, the formula returns "OK"; an empty string otherwise.
Equal to==IF(A2=5, "OK", "Wrong number")If the number in cell A2 is equal to 5, the formula returns "OK"; otherwise the function displays "Wrong number".
Not equal to<>=IF(A2<>5, "Wrong number", "OK")If the number in cell A2 is not equal to 5, the formula returns "Wrong number "; otherwise - "OK".
Greater than or equal to>==IF(A2>=5, "OK", "Poor")If the number in cell A2 is greater than or equal to 5, the formula returns "OK"; otherwise - "Poor".
Less than or equal to<==IF(A2<=5, "OK", "")If the number in cell A2 is less than or equal to 5, the formula returns "OK"; an empty string otherwise.
The screenshot below demonstrates the IF formula with the "Greater than or equal to" logical operator in action:
the IF formula with the

Excel IF function examples for text values

Generally, you write an IF formula for text values using either "equal to" or "not equal to" operator, as demonstrated in a couple of IF examples that follow.

Example 1. Case-insensitive IF formula for text values

Like the overwhelming majority of Excel functions, IF is case-insensitive by default. What it means for you is that logical tests for text values do not recognize case in usual IF formulas.
For example, the following IF formula returns either "Yes" or "No" based on the "Delivery Status" (column C):
=IF(C2="delivered", "No", "Yes")
Translated into the plain English, the formula tells Excel to return "No" if a cell in column C contains the word "Delivered", otherwise return "Yes". At that, it does not really matter how you type the word "Delivered" in the logical_test argument - "delivered", "Delivered", or "DELIVERED". Nor does it matter whether the word "Delivered" is in lowercase or uppercase in the source table, as illustrated in the screenshot below.
Case-insensitive IF formula for text values
Another way to achieve exactly the same result is to use the "not equal to" operator and swap the value_if_true and value_if_false arguments:
=IF(C2<>"delivered", "Yes", "No")

Example 2. Case-sensitive IF formula for text values

If you want a case-sensitive logical test, use the IF function in combination with EXACT that compares two text strings and returns TRUE if the strings are exactly the same, otherwise it returns FALSE. The EXACT functions is case-sensitive, though it ignores formatting differences.
You use IF with EXACT in this way:
=IF(EXACT(C2,"DELIVERED"), "No", "Yes")
Where C is the column to which your logical test applies and "DELIVERED" is the case-sensitive text value that needs to be matched exactly.
Case-sensitive IF formula for text values
Naturally, you can also use a cell reference rather than a text value in the 2nd argument of the EXACT function, if you want to.
Note. When using text values as parameters for your IF formulas, remember to always enclose them in "double quotes".

Example 3. IF formula for text values with partial match

If you want to base your condition on a partial match rather than exact match, an immediate solution that comes to mind is using wildcard characters (* or ?) in the logical_test argument. However, this simple and obvious approach won't work. Many Excel functions accept wildcards, but regrettably IF is not one of them.
A solution is to use IF in combination with ISNUMBER and SEARCH (case-insensitive) or FIND (case-sensitive) functions.
For example, if No action is required both for "Delivered" and "Out for delivery" items, the following formula will work a treat:
=IF(ISNUMBER(SEARCH("deliv",C2)), "No", "Yes")
IF formula for text values with partial match
We've used the SEARCH function in the above formula since a case-insensitive match suits better for our data. If you want a case-sensitive match, simply replace SEARCH with FIND in this way:
=IF(ISNUMBER(FIND("text", where to search)), value_if_true, value_if_false)

Excel IF formula examples for dates

At first sight, it may seem that IF formulas for dates are identical to IF functions for numeric and text values that we've just discussed. Regrettably, it is not so.
Unlike many other Excel functions, IF cannot recognize dates and interprets them as mere text strings, which is why you cannot express your logical test simply as >"11/19/2014" or >11/19/2014. Neither of the above arguments is correct, alas.

Example 1. IF formulas for dates with DATEVALUE function

To make the Excel IF function to recognize a date in your logical test as a date, you have to wrap it in the DATEVALUE function, like this DATEVALUE("11/19/2014"). The complete IF formula may take the following shape:
=IF(C2<DATEVALUE("11/19/2014"), "Completed", "Coming soon")
As illustrated in the screenshot below, this IF formula evaluates the dates in column C and returns "Completed" if a game was played before Nov-11. Otherwise, the formula returns "Coming soon".
An example of the IF formula with the DATEVALUE function

Example 2. IF formulas with TODAY() function

In case you base your condition on the current date, you can use the TODAY() function in the logical_test argument of your IF formula. For example:
=IF(C2<DATEVALUE("11/19/2014"), "Completed", "Coming soon")
Naturally, the Excel IF function can understand more complex logical tests, as demonstrated in the next example.

Example 3. Advanced IF formulas for future and past dates

Suppose, you want to mark only the dates that occur in more than 30 days from now. In this case, you can express the logical_test argument as A2-TODAY()>30. The complete IF formula may be as follows:
=IF(A2-TODAY()>30, "Future date", "")
To point out past dates that occurred more than 30 days ago, you can use the following IF formula:
=IF(TODAY()-A2>30, "Past date", "")
Advanced IF formulas for future and past dates
If you want to have both indications in one column, you will need to use a nested IF function like this:
=IF(A2-TODAY()>30, "Future date", IF(TODAY()-A2>30, "Past date", ""))
A nested IF formula for dates

Excel IF examples for blank, non-blank cells

If you want to somehow mark your data based on a certain cell(s) being empty or not empty, you can either:
  • Use the Excel IF function in conjunction with ISBLANK, or
  • Use the logical expressions ="" (equal to blank) or <>"" (not equal to blank).
The table below explains the difference between these two approaches and provides formula example.

Logical testDescriptionFormula Example
Blank cells=""Evaluates to TRUE if a specified cell is visually empty, including cells withzero length strings.
Otherwise, evaluates to FALSE.
=IF(A1="", 0, 1)
Returns 0 if A1 is visually blank. Otherwise returns 1.
If A1 contains an empty string, the formula returns 0.
ISBLANK()Evaluates to TRUE is a specified cell containsabsolutely nothing - no formula, no empty string returned by some other formula.
Otherwise, evaluates to FALSE.
=IF(ISBLANK(A1), 0, 1)
Returns the results identical to the above formula but treats cells with zero length strings as non-blank cells.
That is, if A1 contains an empty string, the formula returns 1.
Non-blank cells<>""Evaluates to TRUE if a specified cell contains some data. Otherwise, evaluates to FALSE.
Cells with zero length strings are consideredblank.
=IF(A1<>"", 1, 0)
Returns 1 if A1 is non-blank; otherwise returns 0.
If A1 contains an empty string, the formula returns 0.
ISBLANK()=FALSEEvaluates to TRUE if a specified cell is not empty. Otherwise, evaluates to FALSE.
Cells with zero length strings are considerednon-blank.
=IF(ISBLANK(A1)=FALSE, 0, 1)
Works the same as the above formula, but returns 1 if A1 contains an empty string.
The following example demonstrates blank / non-blank logical test in action.
Suppose, you have a date in column C only if a corresponding game (column B) was played. Then, you can use either of the following IF formulas to mark completed games:
=IF($C2<>"", "Completed", "")
=IF(ISBLANK($C2)=FALSE, "Completed", "")
Since there are no zero-length strings in our table, both formulas will return identical results:
The IF formula for blank / non-blank cells

4 ways for changing case in Excel 2013, 2010, 2007

Enter an Excel formula

0. Insert a new (helper) column next to the one that contains the text you want to convert.
Note: This step is optional. If your table is not large, you can just use any adjacent blank column.
Insert a new column to enter formulas in the adjacent blank cells
1. Enter the equal sign (=) and function name (UPPER) in the adjacent cell of the new column (B3).
2. Type in the appropriate cell reference in the parentheses (C3) after the function name.
Your formula should look like this =UPPER(C3), where C3 is the cell in the original column that has the text for conversion.
Enter the equal sign, function name and cell reference to apply the formula to the adjacent cell
3. Click Enter.
Click Enter to see the converted text value
As you can see in the screenshot above, cell B3 contains the uppercase version of the text from cell C3.

Copy a formula down a column

Now you need to copy the formula to other cells in the helper column.
4. Select the cell that includes the formula.
5. Move your mouse cursor to the small square (fill handle) in the lower-right corner of the selected cell until you see a small cross.
Select the cell and drag the fill handle down to copy the formula
6. Hold the mouse button and drag the formula down over the cells where you want it to apply.
7. Release the mouse button.
Release the mouse button to fill the column with the text in uppercase
Note: If you need to fill the new column down to the end of the table, you can skip steps 5-7 and just double-click on the fill handle.

Remove a helper column

So you have two columns with the same text data, but in different case. I suppose you'd like to leave only the correct one. Let's copy the values from the helper column and then get rid of it.
8. Highlight the cells that contain the formula and press Ctrl + C to copy them.
Press Ctrl + C to copy the values from the selected cells
9. Right-click on the first cell in the original column.
10. Click on the Values icon under Paste Options in the context menu.
 Choose Values from the Paste Options in the context menu to insert the text without the formula
Since you need only the text values, pick this option to avoid formula errors later.
11. Right-click the selected helper column and choose the Delete option from the menu.
12. Pick Entire column in the Delete dialog box and click OK.
Select the helper column and choose the Delete option from the right-click menu to remove the column
Here you are!
Delete the helper column to get the table with the converted text case
This theory might look very complicated to you. Take it easy and try to go through all these steps yourself. You'll see that changing case with the use of Excel functions is not difficult at all.

Use Microsoft Word to change case in Excel

If you don't want to mess with formulas in Excel, you can use a special command for changing text case in Word. Feel free to discover how this method works.
1. Select the range where you want to change case in Excel.
2. Press Ctrl + C or right-click on the selection and choose the Copy option from the context menu.
Click Copy or press Ctrl + C to copy the table from Excel to Word
3. Open a new Word document.
4. Press Ctrl + V or right-click on the blank page and select the Paste option from the context menu
 Click Paste or press Ctrl + V to paste the table in Word
Now you've got your Excel table in Word.
5. Highlight the text in your table where you want to change the case.
6. Move to the Font group on the HOME tab and click on the Change Case icon.
7. Pick one of 5 case options from the drop-down list.
Click on the Change Case icon to pick the case you want
Note: You can also select your text and press Shift + F3 until the style you want is applied. Using the keyboard shortcut you can choose only upper, lower or sentence case.
Select the changed table or columns to copy back to Excel
Now you have your table with the text case converted in Word. Just copy and paste it back to Excel.
 Click Paste or press Ctrl + V to paste the changed table back in Excel

Converting text case with a VBA macro

You can also use a VBA macro for changing case in Excel 2010 / 2013. Don't worry if your knowledge of VBA leaves much to be desired. A while ago I didn't know much about it as well, but now I can share three simple macros that make Excel convert text to uppercase, proper or lowercase.
I won't labor the point and tell you how to insert and run VBA code in Excel because it was well described in one of our previous blog posts. I just want to show the macros that you can copy and paste into the code Module.
If you want to convert text to uppercase, you can use the following Excel VBA macro:
?
Sub Uppercase()
    For Each Cell In Selection
        If Not Cell.HasFormula Then
            Cell.Value = UCase(Cell.Value)
        End If
    Next Cell
End Sub
To apply Excel lowercase to your data, insert the code shown below into the Module window.
?
Sub Lowercase()
    For Each Cell In Selection
        If Not Cell.HasFormula Then
            Cell.Value = LCase(Cell.Value)
        End If
    Next Cell
End Sub
Pick the following macro if you want to convert your text values to proper / title case.
?
Sub Propercase()
    For Each Cell In Selection
        If Not Cell.HasFormula Then
            Cell.Value = _
            Application _
            .WorksheetFunction _
            .Proper(Cell.Value)
        End If
    Next Cell
End Sub

Quickly change case with the Cell Cleaner add-in

Looking at the three methods described above you might still think that there is no easy way to change case in Excel. Let's see what the Cell Cleaner add-in can do to solve the problem. Probably, you'll change your mind afterwards and this method will work best for you.
0. Download the add-in and install it on your computer.
After the installation the new Ablebits Data tab appears in Excel.
1. Select the cells where you want to change the text case.
2. Click on the Change Case icon in the Clean group on the Ablebits Data tab.
 Click on the Change Case icon in the Clean group on the Ablebits Data tab to display the Change case section
The Change case pane displays to the left of your worksheet.
3. Select the case you need from the list.
4. Press the Change case button to see the result.
Select the necessary radio button to change the case of text in your Excel table
Note: If you want to keep the original version of your table, check the Back up worksheet box.
 Press the Change case button to convert the text to uppercase in Excel