Search Not Just Numbers

Loading...

Thursday, 21 October 2010

EXCEL TIP: The IF Statement made simple

One of the functions I use most often in Excel is the IF statement. This function is very powerful and to many can seem very complicated, whereas to others it is deceptively simple and its power can be underestimated.

The basic IF statement


The basic format of an IF statement is as follows:

=IF(if this is true,return this,otherwise return this)


Example:

=IF(A2>3,"Greater than three","Not greater than three")

returns the text Greater than three if the number in cell A2 is greater than 3 and Not greater than three, if it is not.

The first argument can be any expression that can be true or false, usually using =,>,<,<=,>= or <>.

Other Examples:

=IF(A3="X","Yes","No")
=IF(SUM(A2:A10)>B2,"Over Budget","Within Budget")
=IF(A2<0,0,A2)

Combining conditions


All of the above examples include only one condition, but it is possible to combine numerous conditions using the AND and OR functions.

Theses functions are formatted as follows:

=AND(Condition 1, Condition 2, Condition 3.....Condition n)
=OR(Condition 1, Condition 2, Condition 3.....Condition n)

The AND function returns TRUE if ALL of the individual conditions are true.

The OR function returns TRUE if ANY of the individual conditions are true.

They can be used in IF statements as follows:

=IF(AND(A2<=200,A2>=100),"In Range","Out if Range")
=IF(OR(A2="X",B2="X",C2="X"),"Contains X","Doesn't Contain X")

More complex decisions (Nested IF Statements)


If the decision required is more complicated, you can have IF statements within IF statements - this is called Nesting.

Example:

=IF(A2=0,"NIL",IF(A2>0,"POSITIVE","NEGATIVE"))

If A2 is 0, this will return the word NIL, however if A is not 0 the third argument is another IF statement that will return the word POSITIVE if A2 is greater than zero, otherwise it will return NEGATIVE.

And that is the IF statement. Don't forget you can still take the easy route and get your spreadsheet built for you at Spreadsheets by Email.


Click here for our our exclusive offer on Online Excel Training 

If you enjoyed this post, go to the top of the blog, where you can subscribe for regular updates and get your free report "The 5 Excel features that you NEED to know".

34 comments:

  1. Thanks for sharing us informative ideas.

    ReplyDelete
  2. Finally, an understandable explanation of the IF statement. Thank you very much!

    ReplyDelete
  3. Julie, pleased you found it useful.

    ReplyDelete
  4. I'm a single dad of 2 age 2 and 5. I find your site so interesting and helpful. I hope I have much time each day to drop by and check your site for recent post. By the way I really appreciate the effort for sharing this.. Thank you..

    ReplyDelete
    Replies
    1. How old are your children now?

      Do you still find this info so interesting?

      Delete
    2. Um, if that was 4 years ago, I'd say his kids are now 6 and 9 (given that neither of them has a birthday between February and October)...

      Delete
  5. The IF function is my go-to as well. It is the first function I turn to to approach any formula. Probably not the best process but that goes to show how big of an impact it has had.

    Good work covering the basics.

    -Grahm
    Excel Statements

    ReplyDelete
  6. One of the clearest (and shortest) overviews on this topic...thank you.

    Mia B.
    excel statement

    ReplyDelete
  7. What if the formula is

    =AVERAGE(IF(R9:BB9>0,6*R7:BB7;R7:BB7))

    Than if R9:BB9 is larger than 0,6xR7:BB7 than it is R7 and otherwise it is BB7?

    ReplyDelete
    Replies
    1. What I think you're trying to do is calculate the average of a range of cells, but use a factor of 6 for each cell in that range where the corresponding value in another range is positive
      If so, the above was very close. You just need to replace the semicolon with a comma and enter the formula as an ARRAY
      Using Arrays is a very powerful technique, much too big to tackle here, but here you just need to type the formula correctly and use Ctrl-Shift-Enter instead of enter
      NB this formula here will NOT ignore empty cells if the condition is met, but average them as if they were zeroes. If the condition is not met then they will be ignored - you could overcome this by using 1*R7:BB7 as the action if false

      rgds, jabche

      Delete
    2. jabche

      I think the posted formula is a non-English localisation so the comma (,)becomes a decimal point (.) and the semi-colon separator (;) becomes the comma (,) as you suggested.

      One thing that is possible as an alternative to CSE data entry is to place the formula in the 'Refers to' box of a new Name (e.g. filtered_average) using Name Manager.

      Then typing
      = filtered_average
      anywhere in the workbook outputs the result.

      Peter

      Delete
    3. Peter,

      if that were true then the first comma would have been a semi-colon too

      or perhaps that one was the misytpe

      jabche

      Delete
  8. Having issues with IF statements for words. Example:
    =IF(B11=nylon, 5, 0)

    Why doesn't this work? Is there a work around? Thanks.

    ReplyDelete
    Replies
    1. When referring to text directly in any Excel formula (rather than by referencing a cell or range containing the text), the text must be enclosed in quotes, e.g.

      =IF (B11="nylon", 5, 0)

      Delete
    2. When referring to text directly in any Excel formula (rather than by referencing a cell or range containing the text), the text must be enclosed in quotes, e.g.

      =IF (B11="nylon", 5, 0)

      Delete
    3. or use a Boolean solution:

      =5*(B11="nylon")

      jabche

      Delete
  9. Hi ya, CAN ANYONE HELP PLEASE?!?

    I have a basic statement that isn’t working:
    =IF(G21=$M$12, “Yes”, “No”) This is in cell CI21
    M12 is an absolute cell while the column G reference will need to change within each row

    G21 is populated by a VB .Value = “”
    M12 is a Validation list

    All cells are set to ‘General’ – the Show Formulas is off – and the Automatic calculation is on

    But it still doesn’t update when the value of M12 is altered from the list?

    I have tried typing long hand with more brackets, copying another IF that does update and amending the cell references, switching the order to M12=G21, creating the formula in a ‘nearer’ cell (incase it was a capacity issue) and checked that all three cells are comunicating with =SUM(G21-M12)… which does work and display the result. I’ve also tried physically selecting the cells rather than typing their references in the formula —- but nothing has worked.

    PLEASE HELP!!

    ReplyDelete
    Replies
    1. The first assumption would be that if your formula tells you the two cells are not the same then it is right. A number may have a small rounding error that prevents a match even when the numbers display the same. Text may have a trailing space or non-printing character. Another possibility is that one of the 'numbers' may actually be a number whilst the other is digits formatted as text.

      By the way, $G$21 strikes me as somewhat small for a validation 'list'.
      Peter

      Delete
    2. make sure calculation isn't set to manual (this has caught me out several times)
      press F9 to force a recalculation

      Jim

      Delete
  10. Dear Alix

    I'm not an expert in VBA, but some other readers are.

    My guess though is that the code is where the problem is. Do you not need to force a recalculation if the data is entered by code.

    Some other readers might have more information for you (please comment if you do!)

    ReplyDelete
  11. is gay need to be a two yr old understand still complex as i'm an Harvard it teacher and my students will understand the it not those in junior or primary (English)

    ReplyDelete
  12. =IF(A2=0,"NIL",IF(A2>0,"POSITIVE","NEGATIVE"))

    If A2 is 0, this will return the word NIL, however if A is not 0 the third argument is another IF statement that will return the word POSITIVE if A2 is greater than zero, otherwise it will return NEGATIVE.

    as per the above example,
    my Query is if the cell is 0 the value be Nil, but if my cell is empty, it is also considering as 0 and place the value as Nil,
    i Required, if the cell is empty the result to be filled as N/A,
    Kindly suggest..

    ReplyDelete
    Replies
    1. Just use the same logic again, leaving your formula as a third argument in another IF:

      =IF (A2="","N/A",IF(A2=0,"NIL",IF(A2>0,"POSITIVE","NEGATIVE")))

      Delete
    2. instead of nesting IFs, I'd enter:

      =IF(ISNUMBER(A2),CHOOSE(SIGN(A2)+2,"negative","nil","positive"),"n/a")

      which will return n/a if A2 is anything but a number (there's never a single "right" answer)

      Jim

      Delete
  13. Hi Glen,

    How do I use IF statement if I have several conditions like below:

    Example:

    Cell A2 - number with 2 decimal places (e.g 2.46)

    I want to categorize the value in column A (A2 to ~) into next cell/column by the below conditions:

    Bin1 = 1.75 ~ 2.01
    Bin2 = 2.01 ~ 2.18
    Bin3 = 2.18 ~ 2.37
    Bin4 = 2.37 ~ 2.57
    Bin5 = 2.57 ~ 2.71

    How do I create an IF statement that contains all 5 conditions? I believe there is a way.

    Regards,
    @tikun

    ReplyDelete
    Replies
    1. @tikun
      You can use Nested IF statements - see this earlier post:

      http://www.notjustnumbers.co.uk/2015/06/excel-tip-nested-if-statements.html

      However, a better solution would be to use a lookup table, as per this post:

      http://www.notjustnumbers.co.uk/2012/10/excel-tip-look-up-commission-or-tax.html

      Delete
    2. Thanks for the feedback Glen.

      Sorry I haven't had the chance to read all of your posts earlier but I am surely will do. :)

      Delete
  14. Here's one for you!

    My order book shows sales orders AND sales returns as positive numbers.

    How would I construct an IF statement that says "if(a2="sales return",make the number in d2 a negative, leave as is)?

    Or is there another way...?

    Thanks in advance :)

    ReplyDelete
    Replies
    1. You need to leave d2 alone and use it in the formula in another column, say column E. So in E2, you would use the formula:

      =IF(a2="sales return",-d2,d2)

      Then use column E as your values.

      Delete
  15. Any advice for me. Seems like formula is only catching IFNA
    =IFNA(IF(AND(B2<0,C2<0),B2-C2)&IF(AND(B2<0,C2>0),B2+C2)&IF(AND(B2>0,C2<0),B2+C2)&IF(AND(B2>0,C2>0),B2-C2),B2)

    ReplyDelete
    Replies
    1. Check your brackets. Your IFNA doesn't close until your final bracket (hence this is just one IFNA function). The rest of your function is just the condition for your IFNA function and it will return B2 if that whole function returns NA.

      Delete
  16. What if it's "-1,900", how can I put that in a IF function?

    THIS IS THE EXACT QUESTION I NEED ANSWERED:

    If the units sold is 5,000 or more above the regional target, the employee gets the value in M9. If the units sold is 1,000 or more above the regional target, the employee gets the value in M8. If the units sold is 500 or more above the target, the employee gets the value in M7, otherwise they get $0.

    ReplyDelete
    Replies
    1. I would recommend calculating the amount above regional target in a separate cell (say N2), then if you want to use the IF statement it would be something like:

      IF(N2>=5000,M9,IF(N2>=1000,M8,IF(N2>=500,M7,0)))

      However, the following poat fives a neater approach to thua kind of problem:

      http://www.notjustnumbers.co.uk/2012/10/excel-tip-look-up-commission-or-tax.html

      Delete