Microsoft Excel 2024 Comprehensive Course — Beginner to Intermediate
X
Excel 2024 Microsoft 365
📗 34 Modules Foundations Data Entry Formatting Formulas Functions Charts Pivot Tables Data Analysis Protection Automation

❓ Module 18: IF Function & Logical Formulas

The IF function is the gateway to intelligent spreadsheets — formulas that make decisions based on data and return different results depending on conditions. An IF formula is the Excel equivalent of asking "if this is true, do that; otherwise, do something else." Once you master basic IF, you will progress to nested IFs, and then to the powerful logical functions AND, OR, NOT, IFS, SWITCH, and IFERROR — which let you test multiple conditions simultaneously and handle errors gracefully. These functions appear in payroll, grading, inventory management, compliance checking, and virtually every analytical spreadsheet.

18.1 The IF Function

IF tests a condition and returns one of two values depending on whether the condition is TRUE or FALSE.

Syntax

=IF(logical_test, value_if_true, value_if_false)
ArgumentWhat It IsExample
logical_testAny expression that evaluates to TRUE or FALSE. Usually a comparison using =, <, >, <=, >=, or <>B2>=50, C2="Pass", A2<>0
value_if_trueWhat the formula returns when the test is TRUE. Can be a number, text in quotes, another formula, or blank ("")"Pass", B2*1.15, 0, ""
value_if_falseWhat the formula returns when the test is FALSE. Optional — if omitted, returns FALSE. Best practice: always include it."Fail", 0, B2, ""

Basic IF Examples

TaskFormulaReturns
Pass/Fail based on a score=IF(B2>=50,"Pass","Fail")"Pass" if B2 ≥ 50; "Fail" if less
Discount if quantity > 10=IF(B2>10,A2*0.9,A2)10% discount if qty > 10; full price otherwise
VAT-exempt or VAT at 15%=IF(C2="Exempt",0,B2*0.15)R0 if Exempt; otherwise 15% VAT
Show nothing if a cell is blank=IF(A2="","",A2*1.15)Blank if A2 empty; VAT-inclusive price if not
Overdue invoice flag=IF(TODAY()>A2+30,"OVERDUE","Current")"OVERDUE" if today is past due date; "Current" if not
Bonus calculation=IF(D2>=$E$1,B2*0.1,0)10% bonus if sales meet target in E1; R0 otherwise

Text vs Number Comparisons

  • When comparing text, enclose it in double quotes: IF(C2="Finance",...)
  • Text comparisons are not case-sensitive: "FINANCE" and "finance" are treated the same
  • When comparing numbers, do not use quotes: IF(B2>50000,...) — using quotes would compare as text and give wrong results
  • To check if a cell is blank: IF(A2="",...) or IF(A2="",...)
  • To check if a cell is not blank: IF(A2<>"",...)

Returning Different Data Types

IF can return text, numbers, dates, formulas, or blank — in any combination in the true and false positions:

=IF(B2>=50, "Pass", "Fail")       — both text
=IF(B2>=50, B2*0.1, 0)         — formula or number
=IF(B2>=50, TODAY(), "")       — date or blank
=IF(B2>=50, SUM(C2:C10), 0)   — function or zero

18.2 Nested IF — Multiple Conditions in Sequence

A nested IF places one IF formula inside another. This lets you test multiple conditions in sequence, assigning a different result to each outcome. Think of it as a chain of "else if" decisions.

Nested IF Syntax

=IF(test1, result1, IF(test2, result2, IF(test3, result3, default_result)))

Excel evaluates the conditions from left to right. As soon as a condition is TRUE, it returns that result and stops. If no condition is TRUE, it returns the last default_result.

Grading Example (5 Grade Bands)

Score in B2:

=IF(B2>=80,"Distinction",
   IF(B2>=70,"Merit",
      IF(B2>=60,"Credit",
         IF(B2>=50,"Pass","Fail"))))

Score 85 → "Distinction"
Score 73 → "Merit"
Score 62 → "Credit"
Score 54 → "Pass"
Score 41 → "Fail"

SA Tax Bracket Example

Taxable income in A2 (simplified brackets):

=IF(A2<=237100, A2*0.18,
   IF(A2<=370500, 42678+((A2-237100)*0.26),
      IF(A2<=512800, 77362+((A2-370500)*0.31),
         IF(A2<=673000, 121475+((A2-512800)*0.36),
            IF(A2<=857900, 179147+((A2-673000)*0.39),
               251258+((A2-857900)*0.41))))))

Note: Always apply the SARS tax tables for current year rates

Nested IF Limitations

  • Maximum 64 levels of nesting in Excel 2007+ (7 in earlier versions)
  • Long nested IFs become difficult to read, audit, and maintain
  • For many conditions, consider IFS (Section 18.5) or SWITCH (Section 18.6) instead
  • Common mistake: testing conditions in the wrong order. Always test from highest to lowest (or lowest to highest) consistently, and test each range boundary precisely.
Order Matters in Nested IF: Always test conditions from one extreme to the other. For grades, test from highest down (≥80, then ≥70, then ≥60...). If you tested ≥50 first, a score of 85 would return "Pass" and never reach "Distinction". Excel stops at the first TRUE condition — more specific conditions must come first.

18.3 The AND Function

AND returns TRUE only when all of its conditions are TRUE. If even one condition is FALSE, AND returns FALSE. It is used inside IF to test multiple conditions that must all be met simultaneously.

Syntax

=AND(logical1, [logical2], [logical3], …)

AND Examples — Standalone

=AND(B2>=50, C2>=50)     → TRUE only if BOTH B2 and C2 are 50 or more
=AND(A2="Active", B2>0) → TRUE only if account is Active AND has a positive balance
=AND(B2>=18, B2<=65)   → TRUE only if age is between 18 and 65 (inclusive)

AND Inside IF — Practical Examples

TaskFormula
Pass only if BOTH theory AND practical ≥ 50=IF(AND(B2>=50,C2>=50),"Pass","Fail")
Bonus only if sales >= target AND attendance ≥ 90%=IF(AND(D2>=$E$1,E2>=90%),"Bonus","No bonus")
Apply discount if quantity > 10 AND customer is VIP=IF(AND(B2>10,C2="VIP"),A2*0.8,A2)
Flag record if name not blank AND amount > 0=IF(AND(A2<>"",B2>0),"Complete","Incomplete")
Working age check (18–65)=IF(AND(C2>=18,C2<=65),"Eligible","Not eligible")

18.4 The OR Function

OR returns TRUE when at least one of its conditions is TRUE. It only returns FALSE when ALL conditions are FALSE. Use OR when any one of multiple criteria is sufficient.

Syntax

=OR(logical1, [logical2], [logical3], …)

AND vs OR — The Key Difference

FunctionReturns TRUE WhenReturns FALSE When
ANDALL conditions are TRUEAny ONE condition is FALSE
ORANY ONE condition is TRUEALL conditions are FALSE

OR Inside IF — Practical Examples

TaskFormula
Flag if either field is blank (data quality)=IF(OR(A2="",B2=""),"Incomplete","Complete")
Exempt if category is "Gift" or "Export"=IF(OR(C2="Gift",C2="Export"),0,B2*0.15)
Approve if manager OR director signs off=IF(OR(D2="Manager",D2="Director"),"Approved","Pending")
Weekend check (day 6=Sat or day 7=Sun)=IF(OR(WEEKDAY(A2,2)=6,WEEKDAY(A2,2)=7),"Weekend","Weekday")
Priority if amount > R100k OR overdue > 60 days=IF(OR(B2>100000,C2>60),"HIGH","Normal")

18.5 The NOT Function

NOT reverses a logical value — TRUE becomes FALSE and FALSE becomes TRUE. It is used when it is easier to define what something is not than what it is.

Syntax

=NOT(logical)

NOT Examples

FormulaEquivalent ToPractical Use
=NOT(A2="Active")=A2<>"Active"TRUE when status is anything except "Active"
=IF(NOT(ISBLANK(A2)),A2*1.15,"")=IF(A2<>"",A2*1.15,"")Calculate only for non-blank cells
=IF(NOT(AND(B2>=50,C2>=50)),"Fail","Pass")=IF(OR(B2<50,C2<50),"Fail","Pass")NOT(AND) = OR of the opposites — logically equivalent

NOT is less commonly used alone but becomes useful when combined with ISBLANK, ISNUMBER, ISTEXT, ISERROR, or complex AND conditions that are easier to express as their negative.

18.6 The IFS Function (Excel 2019+)

IFS tests multiple conditions in sequence and returns the result of the first TRUE condition. It replaces long nested IF chains with a cleaner, flat structure — far easier to read and maintain.

Syntax

=IFS(test1, value1, test2, value2, test3, value3, …)
  • Each pair of arguments is: condition → result if that condition is TRUE
  • IFS stops at the first TRUE condition and returns its value
  • If no condition is TRUE, IFS returns #N/A. To add a catch-all default, end with: TRUE, "default value"

IFS vs Nested IF — Same Grading Formula

Nested IF (harder to read):
=IF(B2>=80,"Distinction",IF(B2>=70,"Merit",IF(B2>=60,"Credit",IF(B2>=50,"Pass","Fail"))))

IFS (clean and readable):
=IFS(B2>=80,"Distinction", B2>=70,"Merit", B2>=60,"Credit", B2>=50,"Pass", TRUE,"Fail")

More IFS Examples

TaskFormula
Leave entitlement by years of service =IFS(C2>=10,25, C2>=5,20, C2>=1,15, TRUE,0)
10+ years = 25 days; 5+ = 20; 1+ = 15; less = 0
Credit rating category =IFS(B2>=750,"Excellent", B2>=650,"Good", B2>=550,"Fair", TRUE,"Poor")
Delivery priority by days overdue =IFS(D2>30,"URGENT", D2>14,"HIGH", D2>7,"MEDIUM", TRUE,"LOW")

18.7 The SWITCH Function (Excel 2019+)

SWITCH compares a single expression against a list of values and returns the result paired with the first match. Unlike IFS which evaluates conditions, SWITCH tests one value for exact equality against alternatives — like a lookup table without VLOOKUP.

Syntax

=SWITCH(expression, value1, result1, [value2, result2], …, [default])

SWITCH Examples

TaskFormula
Translate day number to day name =SWITCH(WEEKDAY(A2,2), 1,"Monday", 2,"Tuesday", 3,"Wednesday", 4,"Thursday", 5,"Friday", 6,"Saturday", 7,"Sunday")
Department code to full name =SWITCH(B2,"FIN","Finance","HR","Human Resources","OPS","Operations","IT","Information Technology","Unknown")
Month number to quarter =SWITCH(MONTH(A2), 1,"Q1",2,"Q1",3,"Q1", 4,"Q2",5,"Q2",6,"Q2", 7,"Q3",8,"Q3",9,"Q3", 10,"Q4",11,"Q4",12,"Q4")
When to use SWITCH vs IFS: Use SWITCH when you are checking one value for exact equality against a fixed list of options (codes, names, numbers). Use IFS when you need range comparisons (>, <, ≥, ≤) or different conditions per test.

18.8 IFERROR and IFNA — Handling Formula Errors

Formula errors like #N/A, #DIV/0!, #VALUE!, and #REF! are distracting and can cascade through a spreadsheet. IFERROR and IFNA intercept these errors and replace them with a friendlier alternative.

IFERROR — Catch Any Error

=IFERROR(value, value_if_error)
FormulaWhat It Does
=IFERROR(A1/B1, 0)Returns 0 instead of #DIV/0! when B1 is zero
=IFERROR(VLOOKUP(A2,$F$2:$G$50,2,0), "Not found")Returns "Not found" instead of #N/A when the value is missing
=IFERROR((B2-A2)/A2, "")Returns blank instead of #DIV/0! when A2 is zero
=IFERROR(INDEX(MATCH(...)), "No match")Returns "No match" when INDEX/MATCH finds nothing

IFNA — Catch #N/A Only

=IFNA(value, value_if_na)

IFNA catches only #N/A errors (the "not found" error). Other errors like #DIV/0! or #VALUE! are still displayed. Use IFNA instead of IFERROR when you want to see other errors (they might indicate real data problems) but hide "not found" gracefully.

=IFNA(VLOOKUP(A2,$F$2:$G$50,2,0),"Not in list") — hides #N/A only
If VLOOKUP has a #VALUE! error (wrong type), it still shows — alerting you to a data problem
IFERROR vs IFNA — Which to Choose:
Use IFERROR when you want to suppress all errors and always show a clean result to end users.
Use IFNA when your formula might produce real errors (#DIV/0!, #VALUE!) that should remain visible for debugging, but #N/A (not found) should be user-friendly.

18.9 Combining Logical Functions

The real power emerges when you combine AND, OR, NOT, and IFERROR with IF in the same formula to handle complex real-world decision logic.

IF + AND + OR Together

Bonus if: (sales >= target AND dept = "Sales") OR (rating = "Exceptional")

=IF(OR(AND(D2>=$E$1,C2="Sales"),F2="Exceptional"),B2*0.1,0)

SA Payroll Scenarios Using Logical Functions

UIF contribution (only if not exempt AND salary <= R17,712/month):
=IF(AND(C2<>"Exempt",B2<=17712),B2*0.01,0)

Overtime pay (only for non-management AND hours > 45):
=IF(AND(D2<>"Management",E2>45),(E2-45)*F2*1.5,0)

Leave approval (if remaining leave >= days requested AND not probation):
=IF(AND(G2>=H2,C2<>"Probation"),"Approved","Rejected")

Net pay (salary − deductions, protected from negative):
=MAX(0,B2-SUM(C2:F2))

Data Validation Checks Using Logical Functions

Complete record check (name, ID, salary all present):
=IF(AND(A2<>"",B2<>"",C2>0),"✓ Complete","✗ Incomplete")

SA phone number format check (starts with 0, 10 digits):
=IF(AND(LEFT(A2,1)="0",LEN(A2)=10),"Valid","Check format")

Valid salary range (above minimum wage, below R200,000/month):
=IF(AND(B2>=MinWage*160,B2<=200000),"Valid","Review required")

18.10 IS Functions — Testing Cell Types

Excel provides a family of IS functions that return TRUE or FALSE based on the type of content in a cell. They are particularly useful inside IF to handle mixed data types gracefully.

FunctionReturns TRUE WhenPractical Use
=ISBLANK(A2)A2 is completely emptySkip empty rows in a list; identify missing data
=ISNUMBER(A2)A2 contains a numeric value (including dates)Verify numbers before calculating; catch text-stored numbers
=ISTEXT(A2)A2 contains textIdentify text entries in a column that should be numbers
=ISERROR(A2)A2 contains any error value (#N/A, #REF!, etc.)Error detection; counting errors in a range
=ISNA(A2)A2 contains #N/A specificallyDetecting "not found" results from VLOOKUP or MATCH
=ISLOGICAL(A2)A2 contains TRUE or FALSEChecking the output of logical tests
=ISFORMULA(A2)A2 contains a formula (not a static value)Auditing whether a cell is a formula or a hardcoded value

IS Functions in Practice

Only calculate if A2 is a number:
=IF(ISNUMBER(A2), A2*1.15, "Enter a number")

Skip blank cells in a list:
=IF(ISBLANK(A2), "", DATEDIF(A2,TODAY(),"Y"))

Count errors in a range (array formula):
=SUMPRODUCT(--ISERROR(B2:B100))

Flag non-numeric entries in a number column:
=IF(ISNUMBER(A2),"OK","✗ Not a number")

18.11 Quick Self-Check

Q1: Write an IF formula that assigns a grade letter to a score in B2: A (90+), B (80–89), C (70–79), D (60–69), F (below 60).

✓ Using nested IF: =IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C",IF(B2>=60,"D","F")))). Using IFS (cleaner): =IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",B2>=60,"D",TRUE,"F"). Both produce the same result. The conditions are tested from highest down so a score of 92 matches ≥90 first and returns "A" without evaluating the rest. The TRUE at the end of IFS is the catch-all default for anything below 60.

Q2: An employee qualifies for a year-end bonus if their performance rating (column C) is "Exceeds" or "Outstanding" AND they have worked for at least 6 months (column D = months employed). Write the IF formula to calculate 10% of their salary (column B) as bonus, or 0 if not eligible.

✓ =IF(AND(OR(C2="Exceeds",C2="Outstanding"),D2>=6),B2*0.1,0). The inner OR checks whether the rating is either "Exceeds" or "Outstanding". The AND ensures that BOTH the OR condition (good rating) AND the minimum service (6+ months) must both be true. If either fails, the AND is FALSE and the formula returns 0. If both pass, it returns 10% of the salary in B2.

Q3: A VLOOKUP formula in column D sometimes returns #N/A when a product code is not in the price list. Your manager wants the cell to show "Price TBC" instead of the error. Rewrite the formula to handle this.

✓ Wrap the VLOOKUP with IFNA: =IFNA(VLOOKUP(A2,$G$2:$H$100,2,0),"Price TBC"). IFNA catches only the #N/A error (not found) and replaces it with "Price TBC". If there is a genuine formula error (like a #VALUE! from a wrong data type), that still shows — alerting you to a real problem. If you want to catch ALL errors and show "Price TBC" for any issue, use IFERROR instead: =IFERROR(VLOOKUP(A2,$G$2:$H$100,2,0),"Price TBC").

Q4: You have a department code in B2 (possible values: "FIN", "HR", "OPS", "IT", "MKT"). Write a SWITCH formula that translates the code to the full department name. If the code is unknown, show "Unknown Department".

✓ =SWITCH(B2,"FIN","Finance","HR","Human Resources","OPS","Operations","IT","Information Technology","MKT","Marketing","Unknown Department"). SWITCH compares B2 against each value in sequence. When it finds a match, it returns the paired result. The last argument "Unknown Department" is the default returned when none of the codes match. This is much cleaner than nested IFs for exact-match lookups.

Q5: What is the difference between AND and OR? Give a real payroll example where you would use AND versus a real example where you would use OR.

✓ AND returns TRUE only when ALL conditions are true; OR returns TRUE when ANY ONE condition is true. AND example: An employee qualifies for overtime pay only if they are NOT a manager AND worked more than 45 hours that week — BOTH conditions must be met: =IF(AND(C2<>"Manager",D2>45),(D2-45)*E2*1.5,0). OR example: An employee is exempt from the medical aid deduction if they are on contract OR are working less than 20 hours per week — either one is sufficient: =IF(OR(C2="Contract",D2<20),0,B2*0.01).

Q6: A column of data should contain only numbers, but some cells have text entries (data entry errors). Write a formula using an IS function that flags each cell as "OK" or "Not a number".

✓ =IF(ISNUMBER(A2),"OK","✗ Not a number"). ISNUMBER(A2) returns TRUE if A2 contains any numeric value (whole number, decimal, date, time, percentage) and FALSE if it contains text, is blank, or contains an error. The IF then returns "OK" for valid numeric cells and "✗ Not a number" for everything else. To count the total number of text entries across a range, use: =COUNTIF(A2:A100,"*") — this counts cells with any text. Or more precisely: =SUMPRODUCT(--ISTEXT(A2:A100)) counts all text cells including those that don't match a specific pattern.

✓ Module 18 Complete — You Have Learned:

  • IF function — syntax (logical_test, value_if_true, value_if_false); returning text/numbers/formulas/blanks; text vs number comparisons (quotes for text, none for numbers); blank/not-blank tests; 6 practical formula examples
  • Nested IF — placing IF inside IF for multiple conditions; 5-grade grading example; SA tax bracket example; 64-level nesting limit; order matters (test most specific first); limitations of long chains
  • AND function — TRUE only when ALL conditions true; syntax; standalone examples; AND inside IF (5 practical SA examples); the AND + OR combination pattern
  • OR function — TRUE when ANY ONE condition true; AND vs OR comparison table; OR inside IF (5 practical examples including weekend check, VAT exemption)
  • NOT function — reverses logical value; combining with ISBLANK; NOT(AND) = OR of opposites (De Morgan's law)
  • IFS function (Excel 2019+) — flat multi-condition testing; TRUE as catch-all default; IFS vs nested IF comparison; leave entitlement, credit rating, delivery priority examples
  • SWITCH function (Excel 2019+) — exact-match lookup against a list; day name, department code, quarter mapping examples; SWITCH vs IFS (equality vs range comparisons)
  • IFERROR — catch any error type; practical VLOOKUP, division, percentage change error handling; returning 0, "", or custom text
  • IFNA — catch only #N/A errors; when to choose IFNA over IFERROR (preserve other errors for debugging)
  • Combining logical functions — IF + AND + OR together; SA payroll scenarios (UIF exemption, overtime, leave approval, net pay protection with MAX); data validation checks (complete record, phone format, salary range)
  • IS functions — 7 IS functions (ISBLANK, ISNUMBER, ISTEXT, ISERROR, ISNA, ISLOGICAL, ISFORMULA); practical uses for data validation and error handling; SUMPRODUCT(--ISERROR()) for counting errors

← Back to All Modules