+ Reply to Thread
Results 1 to 12 of 12

Issue with too many nested IF statements

  1. #1
    Registered User
    Join Date
    08-05-2008
    Location
    Canada
    Posts
    15

    Issue with too many nested IF statements

    Hi,

    I was wondering if it would be possible for somebody to help me in either recreating a shorter (or working) equation that satisfies 6 conditions, let me explain.

    There are 3 important cells:
    1) an input cell – the user will enter either “a”, “b”, “c”, “d”, “e” or “f” (we will call x)
    2) a variable cell – which will be a number previously generated (we will call z)
    3) the output cell (to be calculated – we will call y)

    Conditions:
    1) If x = “a” then y = z
    2) If (x = “b” AND z >= 2) then y = z else y = 2
    3) If (x = “c” AND z >= 3) then y = 1.5z else y = 3
    4) If (x = “d” AND z >= 4) then y = 2z else y = 4
    5) If (x = “e” AND z >= 6) then y = 2z+4 else y = 6
    6) If (x = “f” AND z >= 12) then y = 3z else y = 12

    Here is one of the variations of equations to which I have used. It obviously does not work as there are more than 7 IF statements but it is the closest to what I think should work.

    =IF(R6="a",I6,
    IF(AND(R6="b",I6>=2),I6,
    IF(AND(R6="b",I6<2),2,
    IF(AND(R6="c",I6>=3),I6*1.5,
    IF(AND(R6="c",I6<3),3,
    IF(AND(R6="d",I6>=4),I6*2,
    IF(AND(R6="d",I6<4),4,
    IF(AND(R6="e",I6>=6),I6*2+4,
    IF(AND(R6="e",I6<6),6,
    IF(AND(R6="f",I6>=12),I6*3,
    IF(AND(R6="f",I6<12),12,"ENTER LETTER")))))))))))

    Those around me have suggested using "VLOOKUP" or "CHOOSE" funcitons but I cannot create a working formula.

    It has been a while and I have still not been able to create a working function. I hope my explanation is clear, any help would be appreciated.

    Thanks

    (I have attached a sample worksheet)
    Attached Files Attached Files
    Last edited by sjak; 08-05-2008 at 09:09 AM.

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675
    Here's one way

    =IF(I6>=LOOKUP(J6,{"a","b","c","d","e","f";0,2,3,4,6,12}),LOOKUP(J6,{"a","b","c","d","e","f";0,2,3,4,6,12}),CHOOSE(MATCH(J6,{"b","c","d","e","f"},0),I6,1.5*I6,2*I6,2*I6+4,3*I6))

  3. #3
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    daddylonglegs...

    I may be wrong, but I think you got your Value If True/Value if False arguments backwards...

    I think, according to the OPs pseudocode, it should be...

    =IF(I6>=LOOKUP(J6,{"a","b","c","d","e","f";0,2,3,4,6,12}),CHOOSE(MATCH(J6,{"b","c","d","e","f"},0),I6,1.5*I6,2*I6,2*I6+4,3*I6),LOOKUP(J6,{"a","b","c","d","e","f";0,2,3,4,6,12}))
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  4. #4
    Registered User
    Join Date
    08-05-2008
    Location
    Canada
    Posts
    15
    Thanks for the quick reply daddylonglegs.

    The formula didn't seem to work - I may have been a little misleading with the attachment since my reference to cells in the post was different than that in the attachment (the attachment above has been fixed).

    As for the formula, I worked out the numbers by hand with the first variable in A6 which is 6.56. Here is how it should look after entering a letter between "a" and "f"

    a = 6.56
    b= 6.56
    c= 9.84
    d=13.12
    e=17.12
    f= 19.68

    "f" worked for the first two examples on the worksheet but not for the third.

    Any ideas?

  5. #5
    Forum Contributor
    Join Date
    04-11-2005
    Location
    London
    Posts
    259
    Another way which might be easier to read/maintain as it pretty well follows the logic that you wrote is to write a short user defined function as follows and place it in a module for that workbook

    Please Login or Register  to view this content.
    Martin
    Martin Short

  6. #6
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Quote Originally Posted by sjak
    Thanks for the quick reply daddylonglegs.

    The formula didn't seem to work - I may have been a little misleading with the attachment since my reference to cells in the post was different than that in the attachment (the attachment above has been fixed).

    As for the formula, I worked out the numbers by hand with the first variable in A6 which is 6.56. Here is how it should look after entering a letter between "a" and "f"

    a = 6.56
    b= 6.56
    c= 9.84
    d=13.12
    e=17.12
    f= 19.68

    "f" worked for the first two examples on the worksheet but not for the third.

    Any ideas?
    Did you see my comment to daddylonglegs' formula?... I think he got the results arguments backwards...

    try instead:

    =IF(A6>=LOOKUP(J6,{"a","b","c","d","e","f";0,2,3,4,6,12}),CHOOSE(MATCH(J6,{"a","b","c","d","e","f"},0),A6,A6,1.5*A6,2*A6,2*A6+4,3*A6),LOOKUP(J6,{"a","b","c","d","e","f";0,2,3,4,6,12}))

  7. #7
    Registered User
    Join Date
    08-05-2008
    Location
    Canada
    Posts
    15
    Hi NBVC

    Sorry, I posted a couple min after yours.

    Your correction is working very well except for a small bug to which I am not familiar enough with the function to change without something blowing up.

    Everything works well except when I enter "f" for the first worksheet example. It outputs 12 instead of 19.68 (3*6.56).

    I think the problem is that column A should always be subject to a calculation unless its output is LESS than the minimum restriction set on it.

    In this case, 6.56 * 3 is > 12 and should read 19.68. The current function sees A6 as 6.56 and automatically thinks it is < 12 and therefore gives an output of 12.

    I hope I am making sense, I appreciate all the help so far.

    sjak

  8. #8
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    It is a little confusing now. I am not sure what your conditions are now.

    Your initial post said If (x = “f” AND z >= 12) then y = 3z else y = 12 where I assume z is A6... In your sheet, as you say, A6 is less than 12, so according to your condition, it should yield 12, not 19.68

    Can you rewrite the actual conditions you have in your initial post. Use the actual cell references instead of x,y,z.

  9. #9
    Registered User
    Join Date
    08-05-2008
    Location
    Canada
    Posts
    15
    Conditions:
    1) If J6 = “a” then K6 = A6
    2) If (J6 = “b” AND A6 >= 2) then K6 = A6 else K6 = 2
    3) If (J6 = “c” AND A6*1.5 >= 3) then K6 = A6*1.5 else K6 = 3
    4) If (J6 = “d” AND A6*2 >= 4) then K6 = A6*2 else K6 = 4
    5) If (J6 = “e” AND A6*2+4 >= 6) then K6 = A6*2+4 else K6 = 6
    6) If (J6 = “f” AND A6*3 >= 12) then K6 = A6*3 else K6 = 12

    Thanks for catching that - sorry for the trouble.

    sjak

  10. #10
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675
    Try

    =MAX(LOOKUP(J6,{"a","b","c","d","e","f";0,2,3,4,6,12}),CHOOSE(MATCH(J6,{"b","c","d","e","f"},0),A6,1.5*A6,2*A6,2*A6+4,3*A6))

  11. #11
    Registered User
    Join Date
    08-05-2008
    Location
    Canada
    Posts
    15
    Working well! Thanks everyone for your input - consider this one solved as I check over my entire list. I will let you know if I find anything.

    What a great resource!

    sjak

  12. #12
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675
    Quote Originally Posted by sjak
    Working well! Thanks everyone for your input - consider this one solved as I check over my entire list. I will let you know if I find anything.
    Thanks for the feedback

    Quote Originally Posted by NBVC
    daddylonglegs...

    I may be wrong, but I think you got your Value If True/Value if False arguments backwards...
    I did indeed, Vito, thanks for the correction................

+ 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