Features of !SEMTools
↑ Навигация по задачам в Excel и возможностям надстройки !SEMTools ↑

IF

IF function in Excel - conditional logic examples
Function categoryLogical functions
VolatilityNon-volatile
Similar functionsIFERROR, ISERR, ISERROR, ISNA

What does the IF function do?

The function checks the truthfulness of the first argument - a calculation or cell value. Depending on the result, calculations will be performed or other values returned.

IF function syntax

=IF(condition, value_if_true, value_if_false)
IF: text or calculation depending on a condition
C2=IF(B2>=50,"Pass","Fail")
ABCDEF
1StudentScoreResultAmountDiscount
2Anna72Pass=IF(B2>=50,"Pass","Fail")12000600=IF(E2>10000,E2*5%,0)
3Oliver41Fail=IF(B3>=50,"Pass","Fail")80000=IF(E3>10000,E3*5%,0)
4Emma=IF(B4="","",IF(B4>=50,"Pass","Fail"))

Text results go in quotes; numbers and formulas don’t. An empty cell counts as zero, so it usually gets its own check: B4="".

The condition is typically a calculation that returns a boolean result (TRUE or FALSE).

Comparison criteria commonly used: '=' (equal to), '>' (greater than), '<' (less than), '<>' (not equal to), '>=' (greater than or equal to), '<=' (less than or equal to). You can compare logical, numeric, and text values with each other.

Comparison operators, AND / OR
C6=IF(AND(A6>=5,A6<=10),"yes","no")
ABC
1ValueConditionResult
215A2>10yes=IF(A2>10,"yes","no")
310A3<>10no=IF(A3<>10,"yes","no")
4LondonA4="london"yes=IF(A4="london","yes","no")
50A5 (number as condition)no=IF(A5,"yes","no")
67AND(A6>=5,A6<=10)yes=IF(AND(A6>=5,A6<=10),"yes","no")
712OR(A7<5,A7>10)yes=IF(OR(A7<5,A7>10),"yes","no")

Text comparison isn’t case-sensitive: “London” = “london”. A 0 as the condition is FALSE, any other number is TRUE. “Greater than X and less than Y” is AND with two comparisons.

The function can also accept numbers as input. Numbers not equal to zero are considered equivalent to TRUE, 0 to FALSE.

Other features of the IF function:

  • If the input value is given as an array, the IF function will process each element of that array.

IF formula examples

Example 1

How to check if a cell contains numbers? This site has a detailed discussion of solving this task, including using the !SEMTools add-in, which provides the fastest solution. But now we'll look at it using IF.

You can check if it contains each of the digits, one by one, and if it doesn't contain any, report that it doesn't contain any. The general pseudo-syntax for checking the truth of multiple values would involve nesting the OR function.

=IF(COUNT(check1, check2, ...), value_if_true, value_if_false)

Here COUNT will return a positive number if at least one of the expressions given to it as input (in parentheses after it) is not equal to zero or error, or 0 if none. And the IF function will treat this result as TRUE if the number is positive, and as FALSE if it equals zero.

To check for the presence of digits in a string, we'll use the SEARCH function.

Let's try to apply the syntax to the example with digits. This is what the formula will look like (don't be surprised by line breaks and spaces - they are ignored in Excel and you can compose complex formulas similarly for convenience).

IF function checking for digits in text
=IF(COUNT(SEARCH({0,1,2,3,4,5,6,7,8,9},A2)),"Has digits","No digits")
IF + COUNT: does the cell contain digits
B2=IF(COUNT(SEARCH({0,1,2,3,4,5,6,7,8,9},A2)),"Has digits","No digits")
AB
1TextCheck
2SKU A-102Has digits=IF(COUNT(SEARCH({0,1,2,3,4,5,6,7,8,9},A2)),"Has digits","No digits")
3no numberNo digits=IF(COUNT(SEARCH({0,1,2,3,4,5,6,7,8,9},A3)),"Has digits","No digits")
4iPhone 15Has digits=IF(COUNT(SEARCH({0,1,2,3,4,5,6,7,8,9},A4)),"Has digits","No digits")

SEARCH looks for each of the 10 digits and returns positions or errors. COUNT counts only the numbers: if at least one digit is found, the result is above zero and IF treats it as TRUE. The same trick checks “if the cell contains any of the words”.

Example 2

How to speed up VLOOKUP by 50,000 times? Using IF and a second VLOOKUP! No, there's no mistake here. You can read in detail about this fascinating phenomenon in the corresponding article about the VLOOKUP function, and here we'll just provide the file and formula involving IF.

This is what its syntax looks like:

=IF(VLOOKUP(A1, $E$1:$E$400000, 1, 1)=A1, VLOOKUP(A1, $E$1:$F$400000, 2, 1), "")
Fast VLOOKUP: approximate match + check
B2=IF(VLOOKUP(A2,$D$2:$D$6,1,1)=A2,VLOOKUP(A2,$D$2:$E$6,2,1),"")
ABCDE
1CodePriceCode (sorted)Price
21050320=IF(VLOOKUP(A2,$D$2:$D$6,1,1)=A2,VLOOKUP(A2,$D$2:$E$6,2,1),"")1010150
31040=IF(VLOOKUP(A3,$D$2:$D$6,1,1)=A3,VLOOKUP(A3,$D$2:$E$6,2,1),"")1030210
41050320
5107095
61090480

The lookup table D:E must be sorted ascending. Approximate VLOOKUP (last argument 1) uses binary search and is thousands of times faster than exact match on hundreds of thousands of rows. But for 1040 it would return the nearest smaller code 1030, so the first VLOOKUP checks that the exact code was found, and otherwise IF returns an empty string.

In human language, the formula means that if the value returned by binary search in the first column equals the search value, we perform a second binary search - returning the second column. If not - return an empty string.

Example 3

Excel versions before 2016 lack the MAXIFS and MINIFS functions. But nothing prevents us from creating their equivalent using array formulas.

Let's say we want to calculate the largest and smallest even number in range A1:A10.

We'll use the ISEVEN function (appeared in Excel 2007) on the array and return the number itself if it's even, and an empty string if it's odd. We'll process the resulting array with MAX and MIN functions:

Largest even number in the range:

={MAX(IF(ISEVEN(A1:A10+0), A1:A10, ""))}

Smallest even number in the range:

={MIN(IF(ISEVEN(A1:A10+0), A1:A10, ""))}
Conditional max and min: ISEVEN
C2=MAX(IF(ISEVEN(A2:A11+0),A2:A11,""))
ABCD
1NumbersLargest evenSmallest even
2722=MAX(IF(ISEVEN(A2:A11+0),A2:A11,""))4=MIN(IF(ISEVEN(A2:A11+0),A2:A11,""))
312
43
518
69
74
815
922
101
116

IF returns the number for even values and an empty string for the rest; MAX and MIN ignore text. In Excel 2019 and earlier enter it with Ctrl+Shift+Enter (the braces appear automatically); in Excel 365 just press Enter.

And if we have Excel 2003 and the ISEVEN function is also unavailable, we use the MOD function, checking the remainder when divided by 2:

Largest even number in the range (Excel 2003)

={MAX(IF(MOD(A1:A10, 2)=0, A1:A10, ""))}

Smallest even number in the range (Excel 2003)

={MIN(IF(MOD(A1:A10, 2)=0, A1:A10, ""))}
The same with MOD
C2=MAX(IF(MOD(A2:A11,2)=0,A2:A11,""))
ABCD
1NumbersLargest evenSmallest even
2722=MAX(IF(MOD(A2:A11,2)=0,A2:A11,""))4=MIN(IF(MOD(A2:A11,2)=0,A2:A11,""))
312
43
518
69
74
815
922
101
116

MOD(number,2)=0 checks for an even number without ISEVEN. The captions above the two Excel 2003 formulas were swapped — fixed: MAX gives the largest, MIN the smallest.

Multiple nested IF functions

Sometimes one IF is not enough. For example, you need to check several options and show a different result for each. In such cases, several nested IF functions are used - one inside another. This is convenient when you need to select text or a value depending on multiple conditions.

For example, the following formula can be used in an Excel document where you automatically substitute the correct declension of the phrase "calendar day" depending on the number of days - for example, in a vacation request or memo.

Example scenario:

An employee fills out a vacation request and in cell M7 specifies the number of days (e.g., 3, 5, 14, etc.).

In English, the word "day" changes depending on the number:

  • 1 → calendar day
  • 2+ → calendar days
=IF(M7=1, "calendar day", "calendar days")
Singular or plural
B2=IF(A2=1,"calendar day","calendar days")
ABC
1DaysPhraseIn text
21calendar day=IF(A2=1,"calendar day","calendar days")for 1 calendar day="for "&A2&" "&B2
33calendar days=IF(A3=1,"calendar day","calendar days")for 3 calendar days="for "&A3&" "&B3
414calendar days=IF(A4=1,"calendar day","calendar days")for 14 calendar days="for "&A4&" "&B4
521calendar days=IF(A5=1,"calendar day","calendar days")for 21 calendar days="for "&A5&" "&B5

The phrase is then glued into the request text with &. In English only 1 takes the singular, so a single IF is enough.

This formula automatically generates the correct phrase based on the number of days:

  • if M7 = 1 → "calendar day"
  • if M7 = 2, 3, 4, 5, etc. → "calendar days"

This phrase can then be used in the request text using concatenation (CONCAT, CONCATENATE or with the ampersand "&").

This eliminates manual text correction in requests and other documents where it's important to follow English grammar rules for numerical declension.

When you have too many IF functions

If you use several nested IF functions, the formula becomes long and inconvenient:

=IF(A1=1, "One", IF(A1=2, "Two", IF(A1=3, "Three", "Other")))

Alternatives to nested IF functions, such as VLOOKUP and CHOOSE, help make formulas simpler, clearer, and easier to edit.

✅ Why use the CHOOSE function

If A1 is simply a number (e.g., 1, 2, 3), the formula can be simplified:

=CHOOSE(A1, "One", "Two", "Three", "Four")

If A1 = 3, the result will be "Three".

🔍 Why use VLOOKUP

If you have a lookup table:

ValueResult
1One
2Two
3Three
4Four

You can use VLOOKUP (vertical lookup in a table):

=VLOOKUP(A1, A2:B5, 2, 0)
Nested IF, CHOOSE and VLOOKUP
B3=IF(A3=1,"One",IF(A3=2,"Two",IF(A3=3,"Three","Other")))
ABCDEFG
1ANested IFCHOOSEVLOOKUPValueResult
21One=IF(A2=1,"One",IF(A2=2,"Two",IF(A2=3,"Three","Other")))One=CHOOSE(A2,"One","Two","Three","Four")One=VLOOKUP(A2,$F$2:$G$5,2,0)1One
33Three=IF(A3=1,"One",IF(A3=2,"Two",IF(A3=3,"Three","Other")))Three=CHOOSE(A3,"One","Two","Three","Four")Three=VLOOKUP(A3,$F$2:$G$5,2,0)2Two
44Other=IF(A4=1,"One",IF(A4=2,"Two",IF(A4=3,"Three","Other")))Four=CHOOSE(A4,"One","Two","Three","Four")Four=VLOOKUP(A4,$F$2:$G$5,2,0)3Three
57Other=IF(A5=1,"One",IF(A5=2,"Two",IF(A5=3,"Three","Other")))#VALUE!=CHOOSE(A5,"One","Two","Three","Four")#N/A=VLOOKUP(A5,$F$2:$G$5,2,0)4Four

All three give the same result for 1–3. For 4 the nested IF has no branch, so it returns “Other”; for 7 CHOOSE and VLOOKUP return errors — wrap them in IFERROR. In the demo the lookup table is F2:G5 (the article’s formula refers to A2:B5).

This is especially convenient when you want to change values directly in the table, not in the formula itself.

🧠 Conclusion

  • IF — good for 2-3 conditions
  • CHOOSE — ideal if you have a sequential number
  • VLOOKUP — convenient when values are stored in a table

Other Logical functions in Excel

IF, AND, OR, NOT, IFERROR, IFS, SWITCH, CHOOSE

See also:

Like the article? Help its author! Buy !SEMTools, it has lots of useful instruments to process text data.