Alternative to nested IF structure (XL2013)

AOB

Well-known Member
Joined
Dec 15, 2010
Messages
660
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Hi all,

I need a formula to "bucket" numerical values into groups / categories

Something along the following lines :

Age
Age Category
1
0 - 2
2
0 - 2
3
3 - 7
5
3 - 7
8
8 - 15
12
8 - 15
17
16 - 30
34
31 - 90
106
90+

<tbody>
</tbody>

















I can do this with a simple nested IF statement á la :

Code:
=IF(A2<3,"0 - 2",IF(A2<8,"3 - 7",IF(A2<16,"8 - 15",IF(A2<31,"16 - 30",IF(A2<91,"31 - 90","90+")))))

But I'm curious to see if there's a better, more efficient, method. I don't really like nested IFs; for a start, there's a limit on the number of IFs you can nest (is it 7?) but also they can be difficult to read and, well, I'd just like to learn a better way.

My initial thoughts were something along the lines of the SWITCH statement in Access, where you evaluate an arbitrary series of statements and return the output for the first statement which returns TRUE - which would be perfect here - but there doesn't seem to be an equivalent in Excel?

There's CHOOSE but with that, one would have to specify the return value for each possible input (i.e. 1, 2, 3,.....,X) rather than value ranges

Any other suggestions?

Thanks!

Al
 
Interesting!

Something like this then?

Code:
=VLOOKUP(A2,{0,"0 - 2";3,"3 - 7";8,"8 - 15";16,"16 - 30";31,"31 - 90";91,"90+"},2,TRUE)

Seems to work and doesn't require data to be held elsewhere

How might one adapt this for negative numbers, given I wouldn't have a lower bound?

Only way I can think would be to wrap it inside an IFERROR statement (i.e. lookup fails because no such approximate matching value exists in the array)

Code:
=IFERROR(VLOOKUP(A2,{0,"0 - 2";3,"3 - 7";8,"8 - 15";16,"16 - 30";31,"31 - 90";91,"90+"},2,TRUE),"Negative")

And then handle blanks / invalid cell values with ISNUMBER :

Code:
=IF(ISNUMBER(A2),IFERROR(VLOOKUP(A2,{0,"0 - 2";3,"3 - 7";8,"8 - 15";16,"16 - 30";31,"31 - 90";91,"90+"},2,TRUE),"Negative"),"")

Appreciate it's better to maintain the array somewhere but I quite like the "self-containedness" of this...

Thanks!
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Appreciate it's better to maintain the array somewhere but I quite like the "self-containedness" of this...

You probably won't when you have to amend the bands. ;)
 
Upvote 0
:LOL:

Very fair point, of course, but the bands won't change as they are standardised

To be honest, even if they did, it's not that big a deal to update the formula?

But I do take your point
 
Upvote 0
No problem AOB,

I just implemented what Rory suggested and give the formula below.

=VLOOKUP(D2,{0,"0-2";3,"3-7";8,"8-15";16,"16-30";31,"31-90";91,"90+"},2,TRUE)

Kind regards

Saba
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,822
Members
449,190
Latest member
rscraig11

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top