Expanding Nested IF

lisa6538

New Member
Joined
Oct 26, 2016
Messages
3
I have a nested IF statement that I need to expand if a new column is added which happens often. Wondering if there is a better way to handle this?

Here's my formula
=IF($A$4=$D$4,$D7,IF($A$4=$E$4,$E7,IF($A$4=$F$4,$F7)))

If works perfectly, except I will be adding a column G next month and H the following month, etc. I tried to nest an OR in the IF statement which works for the logical test, but not for the value if true.

Any suggestions would be appreciated.

Thank you!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
If the pattern remains the same, here is a formula. It should be entered using Ctrl+Shift+Enter, not just Enter.

=INDEX($D$7:$H$7,MATCH(1,--($D$4:$H$4=$A$4),0))
 
Upvote 0
Hi Lisa,

Welcome to the forums.

Can you please post a sample of your data that way we can see what's going on so we know exactly what it is that you are trying to achieve?

Also, how many columns are you planning on adding, will there be an end to it?

Or maybe what sheetspread was saying...

Code:
=HLOOKUP(A4,$D$4:$XFD$7,4,0)
 
Last edited:
Upvote 0
Hi and welcome to the MrExcel Message Board.

This seems to work: =HLOOKUP(A4,D4:H7,4,0)

It performs a horizontal look up matching A4 with the data in D4:H7. If it finds an exact match it returns the value from the 4th row in the Range i.e. row 7.
Instead of H7 you could specify the end of the Range much farther out e.g. ZZ7. It depends on how far you want to go.

If you want to return FALSE like the nested IFs when the value is not found you could use: =IFERROR(HLOOKUP(A4,D4:ZZ7,4,0),FALSE)



Regards,
 
Upvote 0
Using Excel 2013. The data in columns D-L will change on rows 7-21. There could be more columns based on how many months and years we are reporting on. Here's my spreadsheet. The IF formula is in Column B =IF($A$4=$D$4,$D7,IF($A$4=$E$4,$E7,IF($A$4=$F$4,$F7))). Everything else is static data.



[TABLE="width: 768"]
<tbody>[TR]
[TD]
A​
[/TD]
[TD]
B​
[/TD]
[TD]
C​
[/TD]
[TD]
D​
[/TD]
[TD]
E​
[/TD]
[TD]
F​
[/TD]
[TD]
G​
[/TD]
[TD]
H​
[/TD]
[TD]
I​
[/TD]
[TD]
J​
[/TD]
[TD]
K​
[/TD]
[TD]
L​
[/TD]
[/TR]
[TR]
[TD="align: right"]2016
[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]2016
[/TD]
[TD="align: right"]2016
[/TD]
[TD="align: right"]2016
[/TD]
[TD="align: right"]2016
[/TD]
[TD="align: right"]2016
[/TD]
[TD="align: right"]2016
[/TD]
[TD="align: right"]2016
[/TD]
[TD="align: right"]2016
[/TD]
[TD="align: right"]2016
[/TD]
[/TR]
[TR]
[TD="align: right"]9
[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]9
[/TD]
[TD="align: right"]8
[/TD]
[TD="align: right"]7
[/TD]
[TD="align: right"]6
[/TD]
[TD="align: right"]5
[/TD]
[TD="align: right"]4
[/TD]
[TD="align: right"]3
[/TD]
[TD="align: right"]2
[/TD]
[TD="align: right"]1
[/TD]
[/TR]
[TR]
[TD="align: right"]2025
[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]2025
[/TD]
[TD="align: right"]2024
[/TD]
[TD="align: right"]2023
[/TD]
[TD="align: right"]2022
[/TD]
[TD="align: right"]2021
[/TD]
[TD="align: right"]2020
[/TD]
[TD="align: right"]2019
[/TD]
[TD="align: right"]2018
[/TD]
[TD="align: right"]2017
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Footnote Formula
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Line 7
[/TD]
[TD]a
[/TD]
[TD][/TD]
[TD]a
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD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]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Line 16
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Line 17
[/TD]
[TD]d
[/TD]
[TD][/TD]
[TD]d
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Line 18
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]c
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Line 19
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]c
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Line 20
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Line 21
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
I tried the Index Match with the array that Tetra101 suggested and it works. I can't figure out the HLookup though.
 
Upvote 0
I can't test until later, but doesn't =INDEX($D$7:$H$7,MATCH($A$4,$D$4:$H$4,0)) without ctrl-shift-enter or the hlookups that Cyrus and Rick posted have the same effect?
 
Upvote 0
Now I have seen the data, I think the INDEX/MATCH solution will be better.

It looks as if the formula is to be placed in cell B7 and then dragged down.
The HLOOKUP formula would be something like: =HLOOKUP($A$4,$D$4:$ZZ7,ROW(B7)-3,0)

However, that inserts a zero where empty cells are looked up. A workaround will be required for that.


Regards,
 
Upvote 0
... doesn't =INDEX($D$7:$H$7,MATCH($A$4,$D$4:$H$4,0)) without ctrl-shift-enter or the hlookups that Cyrus and Rick posted have the same effect?

They have to.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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