+ Reply to Thread
Results 1 to 19 of 19

Nested if and formula

  1. #1
    Forum Contributor
    Join Date
    08-25-2013
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2013
    Posts
    205

    Nested if and formula

    Can anyone show me how to write a chart to figure out how to write a nested if/and formula?

  2. #2
    Forum Expert Sam Capricci's Avatar
    Join Date
    06-14-2012
    Location
    Palm Harbor, Florida
    MS-Off Ver
    16.85 for Mac MS 365
    Posts
    8,513

    Re: Nested if and formula

    could you explain what you mean by show me how to write a chart to figure out how to write a nested if/and formula?
    if/and statements go like this... =if(and(A2=condition 1,B2=a condition[add more and statements if you want]),then do this, otherwise do that) The thing to remember is that both conditions have to exist for the formula to return a positive result, else you get the otherwise do that result. For if/or either condition can exist for your positive result.
    beyond that you'll need to provide more information.
    Make contributors happy, click on the "* Add Reputation" as a way to say thank you.
    Sam Capricci

  3. #3
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Nested if and formula

    Here is an example with 2 conditions. You can have many more conditions but the exact number depends upon what version of Excel you're using.

    =IF(AND(A1>=10,A1<=25),"Yes","No")

    We're testing that the number in cell A1 is greater than or equal to 10 AND less than or equal to 25. Typically, we would define that as being between 10 and 25 (inclusive).

    If ALL the conditions are met return Yes otherwise, return No.
    Last edited by Tony Valko; 12-25-2015 at 07:46 AM. Reason: reworded for clarity
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  4. #4
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Nested if and formula

    Referring back to your previous IF/AND/THEN thread, list the conditions and relevant outcomes as you did there with your first example, and decide on a final 'ELSE' outcome for anything that doesn't match up with any of the specified criteria.

    Start looking for common factors to pair up criteria with results so that you don't have to test the same criteria multiple times.

    If not sure, ask us for help, generally speaking, if you need more than 3 IF's in one formula, then there probably a better way to do the job.

    The last line of the formula that I provided for you in post #23 of that thread is a compact equivalent of using IF(AND( 8 times!

  5. #5
    Forum Contributor
    Join Date
    08-25-2013
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2013
    Posts
    205

    Re: Nested if and formula

    I'm trying to figure out how to write a nested IF/AND formula with lots of variables so I don't have to keep asking people how to write them. I KNOW this post is similar to another post but PLEASE DON'T DELETE IT.

    Here's my end goal: copy and paste the 12 cells from each example (there are 18 examples) into the twelve cells that start at J2 and end at M4. I'm looking to write a nested IF/AND formula that will give the "expected value" listed next to each example. I have written a formula that can satisfy examples 1&2 and 3&4 individually but I don't know how to successfully combine the two. From there I don't even know how to write examples five through eighteen.

    So my question is THIS:

    Is there a way to write out on a sheet of paper all the possible outcomes and then use that as a guide to write a nested IF/AND formula that satisfies my requirements? If so, what would that diagram look like?

    Highlighted in yellow are all the values that are pertinent to determining the "expected value for that example".
    Attached Files Attached Files
    Last edited by quibilty; 12-25-2015 at 08:15 PM.

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,959

    Re: Nested if and formula

    How do you want the answer shown? I am thinking taht a countifS() would be fgar better suited to this than nested if()

    For instance...
    =COUNTIFS($D:$D,$J2,$E:$E,"W",$F:$F,"<=-151",$G:$G,100)
    gives 3
    I know that is not what you want, but we can work on that
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  7. #7
    Forum Contributor
    Join Date
    08-25-2013
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2013
    Posts
    205

    Re: Nested if and formula

    I don't know much about countifs(). As far as how I want the answer shown the value listed under "expected value" is what I'm trying to get. So a formula written in H2 and then copy/pasted down in H to give the value listed.

    Is there a way to SHOW how one creates a countifs() formula?

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,959

    Re: Nested if and formula

    =countifS() is just an extention of countif(), the syntax is...
    =countif(criteria range, criteria)
    or
    =countifS(criteria range1, criteria1,criteria range2, criteria2,criteria range3, criteria3................)

    In my example, I did a test for
    $D:$D,$J2,............count 1 in column D
    $E:$E,"W",............count "W" in E
    $F:$F,"<=-151", ....count <=-151 in column F
    $G:$G,100)............count how many 100 in column G
    so when all these are combined, it counts entries that match all of those criteria in each row

  9. #9
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,959

    Re: Nested if and formula

    =countifS() is just an extention of countif(), the syntax is...
    =countif(criteria range, criteria)
    or
    =countifS(criteria range1, criteria1,criteria range2, criteria2,criteria range3, criteria3................)

    In my example, I did a test for
    $D:$D,$J2,............count 1 in column D
    $E:$E,"W",............count "W" in E
    $F:$F,"<=-151", ....count <=-151 in column F
    $G:$G,100)............count how many 100 in column G
    so when all these are combined, it counts entries that match all of those criteria in each row

  10. #10
    Forum Contributor
    Join Date
    08-25-2013
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2013
    Posts
    205

    Re: Nested if and formula

    Ah, I see. That won't work for what I'm looking for. I'm looking for ONE value in H and that value will either be:

    (using D2:G4 as an example)

    0, G2, G3, G4, G2+(G3*2), G2+(G4*2), G3+(G4*2), or G2+(G3*2)+(G4*3)



    Oh, and I really messed up in my excel example. I can see why people were having a hard time helping me. So for each example listed pretend all 18 examples are copied/pasted into D2:G4 and that the "expected value" and "how expected value was calculated" are to the right. Also, notice that the yellow highlights have changed.
    Attached Files Attached Files
    Last edited by quibilty; 12-25-2015 at 09:18 PM.

  11. #11
    Forum Contributor
    Join Date
    08-25-2013
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2013
    Posts
    205

    Re: Nested if and formula

    What if we tried this instead: in column H of this worksheet I copied/pasted a formula that was created for me by someone else that is SIMILAR to what I want. Can someone show me how to write that formula as flow chart or diagram so I can reverse engineer that formula and thus learn how to create the formula I'm looking for?
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    08-25-2013
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2013
    Posts
    205

    Re: Nested if and formula

    Or maybe this is easier? Here are all the IF/AND formulas that I need to use (only without the "value if false" values). Is there a way to combine them into one formula?

    #1 IF(AND(D2=1, E2=W, F2>=-150)),G2
    #2 IF(AND(D2=1, E2=W, F2<-150)),0
    #3 IF(AND(D2=1, E2=L, F2<-150, D3<>2)),0
    #4 IF(AND(D2=1, E2=L, F2>=-150, D3<>2),G2
    #5 IF(AND(D2=1, E2=L, F2>=-150, D3=2, E3=W, F3>=-150)),G2+(G3*2)
    #6 IF(AND(D2=1, E2=L, F2>=-150, D3=2, E3=W, F3<-150)),G2
    #7 IF(AND(D2=1, E2=L, F2<-150, D3=2, E3=W, F3>=-150)),G3
    #8 IF(AND(D2=1, E2=L, F2<-150, D3=2, E3=W, F3<-150)),0
    #9 IF(AND(D2=1, E2=L, F2>=-150, D3=2, E3=L, F3>=-150, D4=3, F4>=-150)),G2+(G3*2)+(G4*3)
    #10 IF(AND(D2=1, E2=L, F2>=-150, D3=2, E3=L, F3>=-150, D4=3, F4<-150)),G2+(G3*2)
    #11 IF(AND(D2=1, E2=L, F2<-150, D3=2, E3=L, F3>=-150, D4=3, F4>=-150)),G3+(G4*2)
    #12 IF(AND(D2=1, E2=L, F2<-150, D3=2, E3=L, F3<-150, D4=3, F4<-150)),0
    #13 IF(AND(D2=1, E2=L, F2>=-150, D3=2, E3=L, F3<-150, D4=3, F4>=-150)),G2+(G4*2)
    #14 IF(AND(D2=1, E2=L, F2<-150, D3=2, E3=L, F3<-150, D4=3, F4>=-150)),G4
    #15 IF(AND(D2=1, E2=L, F2>=-150, D3=2, E3=L, F3>=-150, D4<>3)),G2+(G3*2)
    #16 IF(AND(D2=1, E2=L, F2<-150, D3=2, E3=L, F3>=-150, D4<>3)),G3
    #17 IF(AND(D2=1, E2=L, F2<-150, D3=2, E3=L, F3<-150, D4<>3)),0
    #18 IF(AND(D2=1, E2=L, F2>=-150, D3=2, E3=L, F3<-150, D4<>3)),G2
    Last edited by quibilty; 12-26-2015 at 04:39 AM.

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    82,610

    Re: Nested if and formula

    I suggest you focus on the conditions that will lead to an outcome other than 0, then make 0 the final ELSE outcome.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  14. #14
    Forum Contributor
    Join Date
    08-25-2013
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2013
    Posts
    205

    Re: Nested if and formula

    OK. That's good advice, but I'm still not sure where to start... Is there a way to write out this logic and then create an easy chart that will help me create the formula?

  15. #15
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    82,610

    Re: Nested if and formula

    You've done that above, really. Try taking each line, adding a comma at the end and then pasting the next IF statement. Excel should help you with the parenthesis at the end of the formula.

  16. #16
    Forum Contributor
    Join Date
    08-25-2013
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2013
    Posts
    205

    Re: Nested if and formula

    The IF formula doesn't have to be in any specific order???

  17. #17
    Forum Contributor
    Join Date
    08-25-2013
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2013
    Posts
    205

    Re: Nested if and formula

    AliGW, I can't believe that worked, but it did!

    =IF(AND(D2=1, E2="W", F2>=-150),G2, IF(AND(D2=1, E2="W", F2<-150),0,IF(AND(D2=1, E2="L", F2<-150, D3<>2),0,IF(AND(D2=1, E2="L", F2>=-150, D3<>2),G2,IF(AND(D2=1, E2="L", F2>=-150, D3=2, E3="W", F3>=-150),G2+(G3*2),IF(AND(D2=1, E2="L", F2>=-150, D3=2, E3="W", F3<-150),G2, IF(AND(D2=1, E2="L", F2<-150, D3=2, E3="W", F3>=-150),G3,IF(AND(D2=1, E2="L", F2<-150, D3=2, E3="W", F3<-150),0,IF(AND(D2=1, E2="L", F2>=-150, D3=2, E3="L", F3>=-150, D4=3, F4>=-150),G2+(G3*2)+(G4*3),IF(AND(D2=1, E2="L", F2>=-150, D3=2, E3="L", F3>=-150, D4=3, F4<-150),G2+(G3*2),IF(AND(D2=1, E2="L", F2<-150, D3=2, E3="L", F3>=-150, D4=3, F4>=-150),G3+(G4*2),IF(AND(D2=1, E2="L", F2<-150, D3=2, E3="L", F3<-150, D4=3, F4<-150),0,IF(AND(D2=1, E2="L", F2>=-150, D3=2, E3="L", F3<-150, D4=3, F4>=-150),G2+(G4*2),IF(AND(D2=1, E2="L", F2<-150, D3=2, E3="L", F3<-150, D4=3, F4>=-150),G4,IF(AND(D2=1, E2="L", F2>=-150, D3=2, E3="L", F3>=-150, D4<>3),G2+(G3*2),IF(AND(D2=1, E2="L", F2<-150, D3=2, E3="L", F3>=-150, D4<>3),G3,IF(AND(D2=1, E2="L", F2<-150, D3=2, E3="L", F3<-150, D4<>3),0,IF(AND(D2=1, E2="L", F2>=-150, D3=2, E3="L", F3<-150, D4<>3),G2,0))))))))))))))))))

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    82,610

    Re: Nested if and formula

    Glad to have helped! There may well be ways of writing a shorter, more efficient formula, but this is a start for you.

  19. #19
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Nested if and formula

    One way to shorten it would be to test a single common condition first, for example, each of your individual formula contains D2=1, so by testing that first, you could remove it from each of the AND functions, shortening the formula quite a bit

    =IF(D2=1,your mega formula with D2=1 removed from each instance of AND(),0)

    Likewise, E2 should only = W or L so you could split the formula the same way again and remove that part from the AND functions, shortening it again, given that this only leaves one remaining condition in the first 2 AND functions, you could remove AND entirely from those, and one of the IF's as well.

    Hopefully that makes sense, have a play with it and see what you can come up with.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 2
    Last Post: 08-24-2014, 04:56 AM
  2. [SOLVED] Help with a Nested Formula
    By ISI_Med in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-01-2014, 01:28 PM
  3. Replies: 2
    Last Post: 10-08-2009, 09:51 PM
  4. Nested LEN and RIGHT formula
    By Dan27 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-20-2008, 07:53 AM
  5. help on a nested countif/nested if formula
    By vickiemc in forum Excel General
    Replies: 2
    Last Post: 08-13-2008, 08:29 AM
  6. Nested Formula?
    By cwilliams in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-08-2006, 11:20 AM
  7. nested formula
    By Ctech in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-23-2006, 12:35 PM

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