We're aware that some users are experiencing technical issues which the team are working to resolve. See the Community Noticeboard for more info. Thank you for your patience.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

Excel - Nested IF Statement

Options
Hi

I apologise for asking another nested IF statement question but I seem to be going round and round in circles and am failing to resolve :(

I have a list of 'items' and against each item the user can enter a code (between 1 and 10; all good here). After entering the code value they may need to enter a sub-code value. To the right of the sub-code cell I have a confirmation cell displaying an error message if appropriate.

So, codes 2, 3, 6, and 9 require sub-code values. And, each of these has varying sub-code ranges. So:

2 - 01 to 04 inc.
3 - 01 to 17 inc.
6 - 01 to 04 inc.
9 - 01 to 06 inc

Basically if the user enters a code of 2 then I want to display an error if they do not enter a sub-code of 01 to 04 inc.

Additionally, I need to utilise as basic an IF statement as possible. By that I mean I cannot use IFS as I cannot guarantee users will have a version of Excel that supports it, and I cannot use macros/programming as they are disabled in the user community.

My statement is below. D54 is the Code cell, E54 is the sub-code cell, and this statement is in F54.

=IF(D54=2,IF(OR(E54<201,E54>204,"Invalid Sub-Code Entered","")),IF(D54=3,IF(OR(E54<301,E54>317,"Invalid Sub-Code Entered","")),IF(D54=6,IF(OR(E54<601,E54>604,"Invalid Sub-Code Entered","")),IF(D54=9,IF(OR(E54<901,E54>906,"Invalid Sub-Code Entered",""))))))

I get an error.

Also a little unsure when IF OR / AND statements were introduced. If it was Excel 2010 of later I may need to regress to a long winded IF statement.

Any help would be greatly appreciated.
Personal Responsibility - Sad but True :D

Sometimes.... I am like a dog with a bone

Comments

  • rmg1
    rmg1 Posts: 3,159 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    Try this one:-
    =IF(D54=2,IF(OR(E54<201,E54>204),"Invalid Sub-Code Entered",IF(D54=3,IF(OR(E54<301,E54>317),"Invalid Sub-Code Entered",IF(D54=6,IF(OR(E54<601,E54>604),"Invalid Sub-Code Entered"),IF(D54=9,IF(OR(E54<901,E54>906),"Invalid Sub-Code Entered","")))))),"")

    You'd just got your brackets in a muddle.
    :wall: Flagellation, necrophilia and bestiality - Am I flogging a dead horse? :wall:

    Any posts are my opinion and only that. Please read at your own risk.
  • cloud_dog
    cloud_dog Posts: 6,321 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Photogenic
    Hi

    Thanks for that. Certainly got rid of the error.

    Unfortunately, my logic is flawed also as I cannot get the error message to appear if I enter an incorrect sub-code for the 3, 6, or 9 codes :eek:
    Personal Responsibility - Sad but True :D

    Sometimes.... I am like a dog with a bone
  • rmg1
    rmg1 Posts: 3,159 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    According to the logic in your OP, this should work:-
    =IF(OR(AND(D54=2,OR(E54<201,E54>204)),AND(D54=3,OR(E54<301,E54>317)),AND(D54=6,OR(E54<601,E54>604)),AND(D54=9,OR(E54<901,E54>906))),"Invalid Sub-Code","")
    :wall: Flagellation, necrophilia and bestiality - Am I flogging a dead horse? :wall:

    Any posts are my opinion and only that. Please read at your own risk.
  • Try this:

    =IF(AND(D54=2,OR(E54<201,E54>204)),"Invalid Sub-Code Entered",IF(AND(D54=3,OR(E54<301,E54>317)),"Invalid Sub-Code Entered",IF(AND(D54=6,OR(E54<601,E54>604)),"Invalid Sub-Code Entered",IF(AND(D54=9,OR(E54<901,E54>906)),"Invalid Sub-Code Entered",""))))

    I've added AND statements, I think IF, AND and OR statements were introduced well before Office 2010 but I'm not absolutely sure.
  • rmg1
    rmg1 Posts: 3,159 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    They've been around since Excel 95 (which is the version I first started with).
    :wall: Flagellation, necrophilia and bestiality - Am I flogging a dead horse? :wall:

    Any posts are my opinion and only that. Please read at your own risk.
  • Linton
    Linton Posts: 18,153 Forumite
    Part of the Furniture 10,000 Posts Name Dropper Hung up my suit!
    I think you want
    =IF(D54=2,IF(OR(E54<201,E54>204),"Invalid Sub-Code Entered",""),
    IF(D54=3,IF(OR(E54<301,E54>317),"Invalid Sub-Code Entered",""),
    IF(D54=6,IF(OR(E54<601,E54>604),"Invalid Sub-Code Entered",""),
    IF(D54=9,IF(OR(E54<901,E54>906),"Invalid Sub-Code Entered",""),"D54 is not 2 3 6 OR 9"))))
  • WaywardDriver
    WaywardDriver Posts: 546 Forumite
    Seventh Anniversary 500 Posts
    edited 14 September 2018 at 4:39PM
    Alternative to nested IFs which should be easier to maintain if valid codes change in future:

    enter valid values of 100*MainCode+SubCode into adjacent cells in a column
    i.e. 100, 201,202,203,204,301...317...906,1000

    select these cells, right-click, Define Name as OKAY say

    then if MainCode in col A and SubCode in col B, enter formula in C1
    =IF(ISNA(MATCH(100*A1+B1,OKAY,0)),"Invalid Sub-code","")

    and copy down column
  • Heedtheadvice
    Heedtheadvice Posts: 2,765 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    edited 15 September 2018 at 11:23AM
    No time to reply in full just at the moment but sould suggest another solution instead of convoluted if statements! They will be a right bu**er to maintain should you have an error or wish to change subcodes!


    Suggest having lists: for each main code with all the applicable subcodes within each list.
    1 you can use lookup and match functions to do the texts (instead of holding the applicable codes in your if functions) -far easier to read and understand the test;
    2 if you need to change the subcodes (either for testing your worksheet or updates/changes) it is easy to see what is appropriate to each main code;
    3 Reduces errors in the 'coding' aspects such ad you are experiencing.


    (or you could have one big list with every valid entry for each main code in a two column list!.....many ways to skin a cat . Your list or lists could be on a separate sheet and even hidden or protected)
  • cloud_dog
    cloud_dog Posts: 6,321 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Photogenic
    Many thanks to everyone's comments and suggestions, it is very much appreciated.

    As mentioned I think I consciously / sub consciously stuck with IF statements simply to remove the risk of isuues due to older versions of Excel in place. I agree possible ongoing maintenance is an aspect that would benefit from a different approach.
    Personal Responsibility - Sad but True :D

    Sometimes.... I am like a dog with a bone
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 350.8K Banking & Borrowing
  • 253.1K Reduce Debt & Boost Income
  • 453.5K Spending & Discounts
  • 243.8K Work, Benefits & Business
  • 598.7K Mortgages, Homes & Bills
  • 176.8K Life & Family
  • 257.1K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.1K Discuss & Feedback
  • 37.6K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.