Reading history

Excel Lookup and Logical Functions

5 min read 0 views

Learn nested IF and IFS for grades, AND and OR, SUMIF, COUNTIFS and AVERAGEIF, and VLOOKUP, XLOOKUP and INDEX MATCH, with IFERROR for clean results.

In the Excel basics lesson you used SUM, AVERAGE and a simple IF. Real result sheets need more. A teacher must turn marks into SSC letter grades, count how many girls got A+, and look up each student’s name from a roll number. This lesson covers the functions that do these jobs: nested IF and IFS, AND and OR, the conditional totals SUMIF and COUNTIFS, and the lookup functions VLOOKUP, XLOOKUP and INDEX with MATCH.

In this lesson you will learn

  • Nested IF and the IFS function
  • AND, OR and NOT inside IF
  • SUMIF, COUNTIF, AVERAGEIF and COUNTIFS
  • VLOOKUP, XLOOKUP, and INDEX with MATCH
  • Handling errors with IFERROR

You should know Excel basics, including formulas and absolute references.

The Example Sheet

All examples use this small result sheet. Row 1 holds headings; the data is in rows 2 to 7.

     A      B          C        D       E
1    Roll   Name       Gender   ICT     Math
2    101    Rina       F        84      91
3    102    Tanvir     M        67      58
4    103    Mitu       F        92      77
5    104    Arif       M        28      45
6    105    Sadia      F        73      88
7    106    Hasan      M        55      62

Nested IF and IFS

SSC grades follow this scale: 80 and above is A+, 70–79 is A, 60–69 is A−, 50–59 is B, 40–49 is C, 33–39 is D, and below 33 is F. An IF can hold another IF inside it, which is called nesting. Check from the top grade down:

=IF(D2>=80,"A+",IF(D2>=70,"A",IF(D2>=60,"A-",IF(D2>=50,"B",
  IF(D2>=40,"C",IF(D2>=33,"D","F"))))))

That is hard to read. Excel 2019 and later have IFS, which lists test and result pairs in order and returns the first one that is true:

=IFS(D2>=80,"A+",D2>=70,"A",D2>=60,"A-",D2>=50,"B",D2>=40,"C",D2>=33,"D",TRUE,"F")

Results for ICT: Rina A+, Tanvir A-, Mitu A+, Arif F, Sadia A, Hasan B

The final TRUE,”F” works as “otherwise”.

AND, OR and NOT

  • AND(test1, test2, …) is TRUE only if every test is true.
  • OR(test1, test2, …) is TRUE if at least one test is true.
  • NOT(test) flips TRUE and FALSE.
Passed both subjects?
=IF(AND(D2>=33,E2>=33),"Pass","Fail")     → Arif: Fail, everyone else: Pass

Got A+ in at least one subject?
=IF(OR(D2>=80,E2>=80),"Star","")        → Rina, Mitu, Sadia: Star

Counting and Adding With Conditions

=COUNTIF(D2:D7,">=80")                       → 2   students with A+ in ICT
=COUNTIF(C2:C7,"F")                          → 3   girls
=SUMIF(C2:C7,"F",E2:E7)                      → 256 total Math marks of girls
=AVERAGEIF(C2:C7,"M",D2:D7)                  → 50  average ICT mark of boys
=COUNTIFS(C2:C7,"F",D2:D7,">=80")           → 2   girls with A+ in ICT

In SUMIF and AVERAGEIF, the first range is checked against the condition and the last range is the one added up. COUNTIFS and SUMIFS accept several range and condition pairs, and every condition must be met.

Looking Up Values

Suppose you type a roll number in cell H1 and want Excel to show that student’s name and ICT mark.

ABCDE1RollNameGenderICTMath2101RinaF84913102TanvirM67584103MituF92775104ArifM28456105SadiaF73887106HasanM5562103H1roll to find11. Search down column A for 1032. Move along that row to column 2 (green) and return the value: Mitu=VLOOKUP(H1,$A$2:$E$7,2,FALSE)
H1 = 103

VLOOKUP(lookup_value, table, column_number, FALSE)
=VLOOKUP(H1,$A$2:$E$7,2,FALSE)     → Mitu     (2nd column of the table)
=VLOOKUP(H1,$A$2:$E$7,4,FALSE)     → 92       (4th column: ICT)
  • VLOOKUP searches the first column of the table, going down.
  • The last argument FALSE asks for an exact match. Without it, Excel may return a wrong row.
  • VLOOKUP cannot look to the left of the search column.

Excel 365 and 2021 have XLOOKUP, which is simpler and can look in any direction:

=XLOOKUP(H1,A2:A7,B2:B7)                  → Mitu
=XLOOKUP("Sadia",B2:B7,A2:A7)             → 105   (looks left: name to roll)

In older versions, INDEX and MATCH together do the same job. MATCH finds the position; INDEX returns the value at that position.

=MATCH(103,A2:A7,0)                        → 3   (third row of the range)
=INDEX(B2:B7,MATCH(103,A2:A7,0))           → Mitu

Handling Errors

If the roll number is not in the list, VLOOKUP shows #N/A. Wrap the formula in IFERROR to show a friendly message instead:

=IFERROR(VLOOKUP(H1,$A$2:$E$7,2,FALSE),"Roll not found")

Other common errors: #DIV/0! (dividing by zero or an empty cell), #NAME? (a misspelt function name or text without quotes) and #REF! (a formula points to a deleted cell).

Common Mistakes

  1. Testing the lowest grade first. In a nested IF, check D2>=80 before D2>=33, or everyone above 33 gets D.
  2. Leaving out FALSE in VLOOKUP. Always use FALSE (or 0) for exact matches such as roll numbers.
  3. Not fixing the table range. Use $A$2:$E$7 so the range does not shift when you copy the formula down.
  4. Forgetting quotes. Conditions and text results such as “>=80” and “Pass” need double quotes.

Practice Exercises

Exercise 1. Write a formula that counts boys who scored below 60 in Math.

Show answer

=COUNTIFS(C2:C7,”M”,E2:E7,”<60″) gives 2: Tanvir (58) and Arif (45). Hasan has 62.

Exercise 2. Write a formula in F2 that shows “Scholarship” if both ICT and Math are 80 or more, and is blank otherwise. Who qualifies?

Show answer

=IF(AND(D2>=80,E2>=80),”Scholarship”,””). Only Rina (84 and 91) qualifies. Mitu misses out because her Math mark is 77.

Exercise 3. Write a VLOOKUP that returns the Math mark for the roll number in H1.

Show answer

=VLOOKUP(H1,$A$2:$E$7,5,FALSE). Math is the 5th column of the table.

Frequently Asked Questions

What is the difference between VLOOKUP and XLOOKUP?

VLOOKUP searches only the first column of a table and returns a value from a column number to its right. XLOOKUP takes a separate search range and return range, so it can look left or right, defaults to an exact match and can show a custom message when nothing is found. XLOOKUP needs Excel 365 or 2021 or later.

How do I calculate grades in Excel?

Use a nested IF or the IFS function, checking from the highest grade down. For example, IFS(D2 greater than or equal to 80, A+, D2 greater than or equal to 70, A, and so on), ending with TRUE and F for everything else.

Summary

  • Nested IF or IFS turns marks into grades; check the highest band first.
  • AND, OR and NOT combine tests inside IF.
  • SUMIF, COUNTIF, AVERAGEIF and COUNTIFS total, count and average with conditions.
  • VLOOKUP (with FALSE), XLOOKUP, or INDEX with MATCH find values in a table; IFERROR hides error codes.

Next: Excel PivotTables and Data Analysis.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *