IF and COUNTIF Formulas in Excel Explained

Formulas & functions · Updated November 23, 2026

The IF function tests a condition and returns one value if true and another if false, while COUNTIF counts cells that meet a single criterion. For example, =IF(A1>10,"High","Low") returns “High” if A1 is greater than 10, and =COUNTIF(B2:B20,">10") counts how many cells in that range are above 10.

What Is the IF Function?

The IF function is one of Excel’s most used logical functions. It evaluates a condition and returns a specified value when the condition is TRUE, and a different value when it is FALSE. The syntax is:

=IF(logical_test, value_if_true, [value_if_false])

The logical_test can be any expression that returns TRUE or FALSE, such as A1>=100, B2="Paid", or SUM(C1:C10)>500. The value_if_true and value_if_false can be numbers, text (in double quotes), formulas, or even other functions. If you omit value_if_false, Excel returns FALSE when the condition is not met.

What Is the COUNTIF Function?

COUNTIF counts the number of cells in a range that meet a single condition. Its syntax is:

=COUNTIF(range, criteria)

The range is the group of cells to evaluate, and criteria defines which cells to count. Criteria can be a number, text, expression, or cell reference. For example, =COUNTIF(A1:A10,"Apple") counts cells containing “Apple”, while =COUNTIF(B1:B10,">=50") counts cells with values 50 or greater.

How to Use IF and COUNTIF Together

You can combine IF and COUNTIF to perform conditional logic based on counts. For instance, to flag rows where a product appears more than once in a list:

Assume product names are in A2:A20. In B2, enter:

=IF(COUNTIF($A$2:$A$20,A2)>1,"Duplicate","Unique")

This checks if the count of the current product in the entire range is greater than 1. If yes, it returns “Duplicate”; otherwise “Unique”. Copy the formula down. This is a common way to highlight duplicates without using Conditional Formatting.

COUNTIFS and IFS: When You Need Multiple Conditions

If your task requires more than one condition, use COUNTIFS (plural) or the IFS function. COUNTIFS counts cells that meet multiple criteria across different ranges:

=COUNTIFS(range1, criteria1, range2, criteria2)

For example, to count orders where the product is “Widget” and the quantity is greater than 10, with product in A2:A20 and quantity in B2:B20:

=COUNTIFS(A2:A20,"Widget",B2:B20,">10")

IFS is an alternative to nested IFs for multiple conditions. It returns a value corresponding to the first TRUE condition:

=IFS(condition1, value1, condition2, value2, ...)

For example, to assign a grade based on a score in C2:

=IFS(C2>=90,"A",C2>=80,"B",C2>=70,"C",TRUE,"F")

Note: IFS is available in Excel 2019 and later, and in Microsoft 365.

Common Errors and How to Fix Them

  • #NAME? – Usually means you misspelled a function name or used text without quotes. Check that function names are correct and text criteria are in double quotes.
  • #VALUE! – Often occurs when the criteria argument is not valid, like using a formula that returns text where a number is expected.
  • Wrong counts with text – COUNTIF is not case-sensitive by default. To count case-sensitive, you need SUMPRODUCT with EXACT, but that is advanced.
  • Criteria with operators – When using comparison operators, enclose the whole expression in quotes: ">10", not >10.
  • Using COUNTIF on non-contiguous ranges – COUNTIF only works on a single contiguous range. For multiple ranges, use COUNTIFS or combine with SUMPRODUCT.

IF and COUNTIF vs. Other Functions

Excel offers several related functions. SUMIF and SUMIFS sum values based on criteria, AVERAGEIF and AVERAGEIFS average values, and COUNTBLANK counts empty cells. For more advanced logic, you might use IFERROR to handle errors, or IFS for multiple conditions without nesting. For counting unique values, use SUMPRODUCT or the newer UNIQUE function.

Practical Examples and Tips

Here are a few real-world scenarios:

  • Sales commission: If a salesperson’s total sales in B2 exceed $10,000, give 10% commission, else 5%: =IF(B2>10000, B2*0.1, B2*0.05)
  • Inventory reorder: If stock in C2 is below 20, flag “Reorder”: =IF(C2<20,"Reorder","")
  • Attendance: Count how many times a student was absent using a list of names in A2:A100: =COUNTIF(A2:A100,"John")

When using IF with text, remember to enclose text in double quotes. Also, when comparing text, use the exact spelling; COUNTIF is not case-sensitive, but it does match entire cell contents unless you use wildcards like * (any sequence) or ? (single character).

Frequently Asked Questions

Can I use COUNTIF with a cell reference as the criteria?

Yes. For example, if D1 contains the value you want to count, use =COUNTIF(A1:A10,D1). If you need to use a comparison operator with a cell reference, concatenate: =COUNTIF(A1:A10,">"&D1).

Why does COUNTIF return 0 even though I see matching values?

This often happens when the criteria are text but the cells contain numbers, or vice versa. Also, check for extra spaces or non-printing characters. Use TRIM to clean data, or ensure the criteria match the data type exactly.

How do I count cells that are not equal to a value?

Use <> in the criteria. For example, =COUNTIF(A1:A10,"<>Apple") counts all cells that do not equal "Apple". This includes empty cells, so if you want to exclude blanks, combine with COUNTA or use COUNTIFS with an additional condition.

Can I use IF to return a blank cell instead of 0?

Yes, use "" (empty string) as the value_if_false. For example, =IF(A1>0,A1,"") returns a blank if A1 is not greater than 0. Note that the cell still contains a formula, but it appears empty.

Is there a limit to how many IF functions I can nest?

In older Excel versions, you could nest up to 64 IF functions. In Excel 2019 and later, the limit is 64, but it's better to use IFS or a lookup table for readability and performance.

How do I count cells with text that contains a specific word?

Use wildcards: =COUNTIF(A1:A10,"*apple*") counts cells that contain "apple" anywhere. The asterisk matches any sequence of characters. For a single character, use ?. Wildcards work with text only, not numbers.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.