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

Father’s Day Gift Guide

Brand Post

Bitdefender 2019

Bitdefender’s best-in-class security solutions have been awarded Product of the Year. Get cybersecurity that 500 MILLION users already have and trust!

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Luke Hill

MSI GT75 TITAN

I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it.

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Louise Coady

Brother MFC-L9570CDW Multifunction Printer

The printer was convenient, produced clear and vibrant images and was very easy to use

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

Featured Content

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?