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 Good Gear Guide newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection

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

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Armand Abogado

HP OfficeJet 250 Mobile Printer

Wireless printing from my iPhone was also a handy feature, the whole experience was quick and seamless with no setup requirements - accessed through the default iOS printing menu options.

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?