IT Lecture Notes by Mark Kelly, McKinnon Secondary College
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
How to avoid using nasty nested IF statements in Excel |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Some people insist on doing things the hard way - such as using nested IF statements in Excel to handle multi-condition behaviours. Quite simply - you hardly EVER have to use nested IF constructs... I went Googling to try and find a really good case of where a nested IF
was essential. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CASE 1My first Googling found... http://www.cpearson.com/excel/nested.htm The author then creates a massively complicated IF solution. This is an example of where you just would not use nested IF at all.
Far better than twisting yourself up with IF... |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Another strategyThe CHOOSE() function also lets you easily look up values
for an index value e.g.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CASE 2My second Google led to... To illustrate, let's say you have a spreadsheet that you use to keep track of your sales force. The rate of commission each sales person receives is based on the amount of sales they have generated for that month. For example: From $1 to $10 earns 10% commission Assuming the amount of sales is in column B, starting at row 4, and that the column containing the commission is formated for percentages, this is what the nested IF function would look like: =IF(B4<=10,"10", if(b4<=100, "15", "20")) Once again, a simple VLOOKUP is far superior.
Name the range (excluding headings) "table" and then use (Assuming the value to look up is in a cell named "sales") |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
NOTENote the method I now use for setting out lookup tables. I
used to get
mental cramps working out the cutoff values for categories. Now I use "From" and "To" columns to make it much easier to determine the numbers.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CASE 3Example 3, found at > Answer: You can write a nested IF statement to handle this. For
example: Once again, it's far easier to use ...
Name the range "table" (or whatever) =A1 * VLOOKUP(A1,table,3) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CASE 4Example 4, found at
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TIPIf searching a table for an exact match (e.g. cases 1 and 4) rather than for a value fitting into a continuous range (e.g. values from 26 to 73) you can use the mysterious optional parameter in vlookup... =VLOOKUP(a1,table,2, FALSE) By adding the " ,FALSE", vlookup will look for an exact match for
the lookup value in the table. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
In short: I (nearly) never use nested IFs because usually there are easier and smarter alternatives. Here's a challenge: come up with a neat example where it is essential to use nested IFs! Tell me about it!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The Iron IF Challengers |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() Yeah - I like Iron Chef. Ya wanna make something of it, cowboy? |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CHALLENGE 1A worthy challenge from yangontha on 28 October 2006. If memory serves me correctly, yangontha said: Column A has a property type and the value ranges from 1 to 5.
I am not saying, nested if is necessary. But I can't think of a way, how to put the formula in column D. This is what I replied to yangontha. Allez Excel! Hi Yangontha. Not even a nested IF is required using the sample data you provided.
Mind you, if you added divide and subtract to get four conditions rather than two, a different approach would be needed - but not VLOOKUP in this case - you'd use the CHOOSE function (as mentioned above).
It's not VLOOKUP, perhaps , but I never said VLOOKUP was the answer for everything. You always use the best-fitting solution to a problem: you don't force the one tool you know into solving every problem. Good try , but nested IF is still a beaten foe. :-) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CHALLENGE 2'Curious' wrote on Saturday, December 30, 2006 at 09:51:40 Hi Mark - What about if you have a range of dates formatted in
date form (i.e. 12/29/2006) and you want Excel to search that range of
data and return the month in text form (i.e. DEC). Isn't it necessary to
use a nested IF for this case? I tried using a vlookup formula (quite similar
to the first example listed on your website) and making a table (named
MONTHS) that defines each month (ex. 1 = Jan, 2 = Feb, and so on)... naming
that table and then using the following vlookup formula: =VLOOKUP(A9,MONTHS,2) And I replied: Hi curious. You were SO close! The problem you had was that you had not extracted the month from the date to use in the lookup. VLOOKUP will do the job very nicely, with the addition of one simple MONTH( ) function :-)
The formulas:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CHALLENGE 3On 15 August 2007, Jay wrote to say: Here's 1, I am having trouble coming up with ANY way of solving: Hi Jay. What about this: =IF(COUNTIF(A3:F3,"<85")>=2,"Fail","Pass") It simply counts the number of cells that are less than 85. If there are 2 or more, they fail.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CHALLENGE 424 September 2007 Hi, In sheet two there is a MATRIX TABLE...What i need to do is Bring the proper value from sheet2. Like---- if ID=1 and Bill No=1 then it will show value from B4 in sheet2.
And the result is once again far easier than expected, so long as you know what Excel has in its toolbox for you to play with... the fomula for the total bill is simply:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CHALLENGE 511 October 2007 Sara wrote to say... Hi Mark:
I'm quite intrigued on your battle against the nested ifs!
Here's another challenge...you haven't addressed if the condition meets 2 requirements such as:
=IF(AND(M11="A",J11>(TODAY()-1095)>0),"No","Yes") and so on...I'm driving myself crazy with the ifs....could you help? Take care, This was an interesting challenge, but solvable with a bit of lateral thinking and a nested LOOKUP.
Entering an arbitrary "type" (A-F in this case) into C2 and a "days ago" into C2 gives the value matching that combination of days and type. The formula in F12 is =VLOOKUP(C2,E6:F10,2) + 1 This handles the first step of the two-part lookup - it looks up the "days ago" value in the table and returns a row number (that's the tricky bit). This looks up the "type" in a HLOOKUP and uses the previous VLOOKUP's value to act as the HLOOKUP's row parameter. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Challenge 6 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I just found this one lurking in the bottom of my hard disk. I must have lost it or forgotten to upload it. Anyway, it's an interesting one. Someone wanted to know how to work out salaries that depended on not one but two variables: length of service and education level without the use of tortuous nested IFs. In effect, they wanted to get this...
To solve this required a bit of lateral thinking - instead of a nested IF, we need a nested lookup! First, we do a normal lookup of years of service to find the right row in the salary table. So James, for example, with 6 years of service is looked up in the B5:E9 table and we find he's in row 2 of the table. The hard bit is to then determine which column we need to read from to get his salary; it depends on the degree he holds. This is where the second lookup happens. The "column" parameter of the first lookup will not be a constant - it will be dynamically calculated by another lookup. The second lookup looks up the person's degree in a second table (G6:H8). It returns a number from 2 to 4, which is used to select a column value for the first lookup.
The result is something like this:
Note the extra fourth parameter in the nested lookup - it allows the degrees to be in any order rather than sorted alphabetically. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CHALLENGE 7Loti wrote in 2008... I am an HR Officer responsible for recruiting employees for a Mining company that has a workforce of 1800 employees. I maintain an employee data base in excel. I want to create a column in the database, where I will be able to know if an employee is due for the first probation assessment after 3 months. If, after being assessed, the employee is not ready for confirmation I would like to indicate in the next column that "Emp. is Not Ready" and after another 3 months the employee should be subjected to a 2nd assessment. If after the first assessment, the employee is deemed suitable for the position, I should indicated 'Confirmed" in the next column to close the chapter on that particular employee. What functions can I use to know (1) when an employee is due for the first assessment, (2) if employee is not subjected to the first assessment after 3 months that the 1st assessment is overdue, and (3) when the 2nd assessment is due. I have attached part of the database for you to fully understand my problem. Regards Loti The sample spreadsheet attached had some quite lengthy nested IF statements such as... =IF((DATEDIF(C4,TODAY(),"d"))>80,IF(((DATEDIF(C4,TODAY(),"d")))<91,"Due",IF(((DATEDIF(C4,TODAY(),"d")))<180,IF(ISBLANK(E4),"Over due",""),""))) It took a bit of fiddling and head scratching to work out the logic of the problem. It was interesting because there were several logical tests to consider: How long has the person been employed? Have they been confirmed yet? Have they been assessed yet? Again, I thought, VLOOKUP and his magical pony can save the day. With a relatively simple IF, a VLOOKUP and a super-sneaky IF inside the VLOOKUP we can test for multiple conditions in one sweet package...
Column A is Date of Employment. Note that in this test data I fixed "today" to a particular date so the result would not vary over time. One would use the TODAY( ) function for the real thing. The important column is B - status. If they have not been employed for 90 days, they are not due for assessment. If they have been employed 90 to 179 days, and they have not been assessed, their status is "due for first assessment". If they had been assessed but not confirmed, they would be shown as "Not ready 1". You can see how complex this is getting. Nested IF was simply not up to the task. This is how I did it...
Ignore the fact that DOE entries are manufactured rather than being literal dates. I did this to easily create test data for each possible DOE. Look at column B. If the person has NOT already been confirmed...
If the person HAS been confirmed, it's a simple status of "confirmed". So I hope you can see how once again VLOOKUP can be made to respond to second-level decisions by using different columns for the function's return value. It's similar to a couple of the challenges above. Thanks for the challenge, Loti! Download the Excel solution (Excel 2007 format) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Next challenger please... |
Back to the last page you visited
Created 25 July 2006
Last changed: November 4, 2008 10:31 AM
IT Lecture notes (c) Mark Kelly, McKinnon Secondary College