Excel’s IF statement made simple:
Excel’s IF statement made simple. Though abbreviated, the logic functions in Excel are no harder than any other. Excel is many things: powerful, useful, colorful, handy. But its logic actions can be challenging to beginner due to their implicit logic. And a shorthand syntax designed to fit on a single line in the formula bar. Excel’s IF statement made simple.
In particular, there’s the IF statement, But once you’re familiar with it, it’s a breeze. At least for easy stuff.
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, later 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 bulky. 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, differently cell B1 will equal 3. Plain language requires a lot of typing and space, so it’s common practice to both infer and calligraphy. In the BASIC programming language (not VBA, though it’s related) the logic above would look something like:
IF A1 = 1 THEN
B1 = 2
B1 = 3
Excel’s IF statement made simple continue:
As the word “will” and others can be deduced from context, they are left out. “Equals” has been short-handed to the = symbol. lesser 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 go in into the formula bar before creating a formula. Simply means “this cell equals the result of the following formula”. The true & false outcome are classified hardly 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 observation is false). Or specifically in this case: =IF(A1=1, 2, 3). Nice, neat, compact—if not particularly easy for neophytes.
Things get even harder to read when you want to do one thing. If A1 equals 1, and an other if A1 equals 13. If the 1st test isn’t true, i.e., A1 does not equal 1, the formula falls through to the bogus 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 equivalent 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 positions are true. Such as B1 =2 and C1 = 3, still there are other functions. That are easier such as AND or OR, which I’ll cover in another column. You can extend to nest. IF statements in either field till the cows come home, or, more likely—the formula becomes useless to read. Which happens very hastily. 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 fields 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”)).
That’s it for this time. Next time, I’ll cover the logical operators beyond the equals (=) I coated here, including “greater than” (>), less than (<), etc.