+ Reply to Thread
Results 1 to 13 of 13

IF AND statements

  1. #1
    Registered User
    Join Date
    02-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    IF AND statements

    After a bit of forum surfing I put this formula into my s/sheet =IF(AND(X4:X15="1",X4:X15="2",X4:X15="3"),"OK","ERR") with a view to the result being, that if the cell range X4 to 15 has a 1, a 2 and a 3 in it, all 3 values having to be present) then i get an OK message but if there are only 2 of the values say than I get an ERR message

    i've recently started using IF and nested IF statements and think I kinda have a grasp but the above formula, in a sample dataset where all 3 values are present gives me the ERR message, and if i delete out 1 of the 3 values then i still get the error message

    I'm sure i'm missing something obv but my first game of AND and i'm just not seeing it...............................

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: IF AND statements

    You can't evaluate an entire range like that, try:

    =IF(AND(COUNTIF(X4:X15,1)>0,COUNTIF(X4:X15,2)>0,COUNTIF(X4:X15,3)>0),"OK","ERR")

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: IF AND statements

    no, sorry, different question.

  4. #4
    Registered User
    Join Date
    02-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: IF AND statements

    Thanks Andrew

    I've run that, on the first run, with the 3 needed values already in the cell range X4:X15 it returned "OK", but when i deleted 1 of the three values out it stayed as OK

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: IF AND statements

    Have you got calculation set to automatic or manual? If the latter you'll need to press F9 to recalculate the formula.

  6. #6
    Registered User
    Join Date
    02-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: IF AND statements

    Thanks Andrew

    That's now set to auto and the formula is working, however how do i get it to error if i have more than the 3 values i need, i iwant it to OK just on there being 1,2 & 3 in the range and to error under any other instance.....

  7. #7
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: IF AND statements

    Use = instead of >

    =IF(AND(COUNTIF(X4:X15,1)=0,COUNTIF(X4:X15,2)=0,COUNTIF(X4:X15,3)=0),"OK","ERR")

  8. #8
    Registered User
    Join Date
    02-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: IF AND statements

    zbor, thanks for your input but that's error-ing even with the 3 values in my cell range........

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: IF AND statements

    Are all of your values numeric? If so try:

    =IF(AND(COUNTIF(X4:X15,1)>0,COUNTIF(X4:X15,2)>0,COUNTIF(X4:X15,3)>0,COUNTIF(X4:X15,">3")=0),"OK","ERR")

  10. #10
    Registered User
    Join Date
    02-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: IF AND statements

    Andrew

    Belter! That works perfect. Now I have the formula, can you do me a vavour when you've time? I grasp logic functions quite well, can you explain the countif to me?

    thanks for all your input!

  11. #11
    Registered User
    Join Date
    02-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: IF AND statements

    Also, how do I apply condition formatting to the True/False versions of the result?

  12. #12
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: IF AND statements

    COUNTIF does pretty much what it says on the tin - it counts the number of cells in a range meeting the specified criteria.

    So COUNTIF(A1:A100,3) counts all of the cells in the range A1:A100 which are equal to 3, whereas COUNTIF(A1:A100,">3") counts how many cells in that range are greater than 3 and COUNTIF(A1:A100,"<>3") counts how many cells in the range aren't equal to 3.

    In your particular case, because you want the values of 1,2 and 3 in your range we know that COUNTIF(X4:X15,1) needs to be greater than zero (i.e. at least one cell in that range must contain a value of 1) and COUNTIF(X4:X15,2) also must be greater than 0 and COUNTIF(X4:X15,3) likewise. Because we don't want any other values in there COUNTIF(X4:X15,">3") must be 0. So we put all of those formula together in an AND statement and Bob's yer uncle.

    To get the TRUE/FALSE version just strip it down to the AND statement...

    =AND(COUNTIF(X4:X15,1)>0,COUNTIF(X4:X15,2)>0,COUNTIF(X4:X15,3)>0,COUNTIF(X4:X15,">3")=0)

  13. #13
    Registered User
    Join Date
    02-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: IF AND statements

    brilliant, thanks again for all your help1

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1