Excel's IF statement made easy
Excel is many things: powerful, useful, colorful, handy, but its logic functions can be challenging to newcomers due to their implied logic, and a shorthand syntax designed to fit on a single line in the formula bar.
In particular, there’s the IF statement, which generates by far the most Excel-related queries to Answer Line. But once you’re familiar with it, it’s a breeze. At least for simple stuff.
[Have a tech question? Send your query to [email protected].]
Just like ordering lunch
Logic in programming or formulas is no different from deciding where to go and what to eat for lunch. If Harry’s is open, then we’ll go there, and I’ll have the Caesar’s salad. If Harry’s is closed, then let’s eat at Harriet’s and there’s a turkey burger that’s not too heavy. If both are closed, to heck with it, I’m doubling down on a Whopper meal.
In a formula that’s more relevant to Excel: If this cell, A1, is equal to 1, then cell B1 will equal 2, otherwise cell B1 will equal 3. Plain language requires a lot of typing and space, so it’s common practice to both infer and shorthand. In the BASIC programming language (not VBA, though it’s similar) the logic above would look something like:
IF A1 = 1 THEN
B1 = 2
ELSE
B1 = 3
ENDIF
As the word “will” and others can be deduced from context, they are left out. “Equals” has been short-handed to the = symbol. Fewer words to type, while still easily readable.
Excel condenses things even further. The destination, being the cell the formula is entered into, is also inferred by shorthanding. The “=” you must enter into the formula bar before creating a formula simply means “this cell equals the result of the following formula”. The true and false results are identified merely by their order, as the second and third fields, respectively.
What you wind up with is =IF(the Comparison to make, the value of the cell if the comparison is true, the value of the cell if the comparison is false). Or specifically in this case: =IF(A1=1, 2, 3). Nice, neat, compact—if not particularly easy for neophytes.
Nesting
Things get even harder to read when you want to do one thing if A1 equals 1, and another if A1 equals 13. If the first test isn’t true, i.e., A1 does not equal 1, the formula falls through to the false field. So cleverly, instead of forcing you to return a hard value such as 3, Excel lets you use this false field to test another condition using a nested IF statement. In this case, if A1 is equal to 13: =IF(A1=1, B1=2, IF(A1=13, B1=5, B1=3)).
The last false field in a series of nested IF statements is always the default result returned if none of the conditions are met.
You can also nest IF statements in the true field if you want to test if multiple conditions are true, such as B1 =2 and C1 = 3, though there are other functions that are easier such as AND or OR, which I’ll cover in another column. You can continue to nest IF statements in either field till the cows come home, or, more likely—the formula becomes impossible to read, which happens very quickly. If you want to make complex logic more readable, then you’ll have to delve into Visual Basic for Applications (VBA) and write macros for cells.
Excel provides other powerful, IF-related functions such as COUNTIFS and SUMIFS, and AVERAGEIFS that make it easier to deal with ranges of cells, but they all operate in the same manner.
By the way, the mock Excel formula for lunch would be (more or less): =IF(Harrys=Open, “Caesar’s salad”, IF(Harriets=Open, “Turkey burger”, “Burger King Whopper Meal times two”)).
More stuff
That’s it for this time. Next time, I’ll cover the logical operators beyond the equals (=) I covered here, including “greater than” (>), less than (<), etc.