Excel's IF statement made easy

Though abbreviated, the logic functions in Excel are no harder than any other

IDG

IDG

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.

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.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags ExcelMicrosoftmicrosoft office

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Jon L. Jacobi

PC World (US online)
Show Comments

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Cate Bacon

Aruba Instant On AP11D

The strength of the Aruba Instant On AP11D is that the design and feature set support the modern, flexible, and mobile way of working.

Dr Prabigya Shiwakoti

Aruba Instant On AP11D

Aruba backs the AP11D up with a two-year warranty and 24/7 phone support.

Tom Pope

Dynabook Portégé X30L-G

Ultimately this laptop has achieved everything I would hope for in a laptop for work, while fitting that into a form factor and weight that is remarkable.

Tom Sellers

MSI P65

This smart laptop was enjoyable to use and great to work on – creating content was super simple.

Lolita Wang

MSI GT76

It really doesn’t get more “gaming laptop” than this.

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?