An alternative to nesting Ifs to get results.

almostanexpert

Board Regular
Joined
Apr 20, 2007
Messages
86
Hello All,

Thank you for your time.

I am in need of your assistance in finding a better way, a more efficient non-vba way, of getting the results I get from nesting Ifs. I currently pull data from our main frame and do a visual scan. I developed a crude If statement, =IF(D2=A2,E2,IF(F2=A2,G2,IF(H2=A2,I2,IF(J2=A2,K2,"")))), which as you can see looks at A2 and then goes to the first instance of data and checks to see if it matches, if it does not it goes to the next set of two columns (D:E, F:G, H:I etc...) and compares if the next set proves TRUE. When it does it returns the second column. So if A2 is a match with D2 then it will return what is in E2. This matching will need to occur for approximately 40 sets (80 columns) of information. So as you can see nesting that many Ifs can become a bit difficult.

I have explored Index Match but they don't seem to give me the results I would like and I have also explored SumProduct (my porsonal favorite for complex lookups) but I don't have an intersection to build my formula.

Thank you again.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hello, If I understand correctly, something like this should work, change the range to fit:

=INDEX(E2:L2,MATCH(A2,D2:K2,0))

If there is a possibility there is no match then:

=IFERROR(INDEX(E2:L2,MATCH(A2,D2:K2,0)),"")
 
Last edited:
Upvote 0
Do both cells of a "pair" (D:E, etc) contain the same type of values (numbers or text), so that it's possible that say E2 could match A2 when none of the other data match A2. Can you post a sample of data for just the abbreviated row you refer to in your initial post (A2:I2)?
 
Upvote 0
Hello All,

A B C D E F G H I J K
Drew Drew 2
Drew Drew 4
Drew Drew 8

B2 = 2
B3 = 4
B4 = 8

Where the columns with out information will have information just not the one that matches column A. Sorry folks I do not have what is required to better view the sample I provided. On my first row I had put D2, Drew and E2, 2, then the next row H3, Drew and I3, 4 and the next row F3, Drew and G3, 8.
 
Upvote 0
@Joyner - Your solution worked, could you explain how it does please.

Are you sure it works? I can see why it could work, but I can also see how it might break depending upon the values in your range A2:CE2.

Take this rough example I have put together:

Excel 2010
ABCDEFGHIJKLM
1A=Answer
25539124133

<tbody>
</tbody>
Sheet1

Joyner's formula correctly returns 3 (A2 = D2, thus the formula returns the value in E2).
It is able to return E2 because the reference range in the MATCH part of the formula is offset by 1 column to the right of the reference range in the INDEX part of the formula.

But what about this situation:
Excel 2010
ABCDEFGHIJKLM
1A=Answer
25459124139

<tbody>
</tbody>



What if A2 = E2, if that is the case Joyner's formula will return the value from F2.

But if I understand correctly you don't want to test A2 = E2. You want to test every second value
A2 = D2
A2 = F2
A2 = H2

And so on.

Thus you don't want to compare A3 to E2 (E2 only comes into it if A2 = D2).
Have I understood correctly?
 
Last edited:
Upvote 0
If what I have assumed above is correct (and of course I might have completely misunderstood), how about a formula like this:

=IFERROR(INDEX(E2:L2,MATCH(1,(A2=D2:K2)*(MOD(COLUMN(D2:K2),2)=0),0)),"")

The above is an array formula and must be entered CTRL+SHIFT+ENTER

Note the the INDEX reference range is offset by 1 column from the the comparison range.

The above formula only matches A2 to D2, F2, H2 and so on, but it only returns values from E2, G2, I2 etc.
 
Upvote 0
almostanexpert.

Like Harry's formula, I offset the INDEX and MATCH so the formula returns the value to the right of the lookup/match value. So if the match to A2 is F2, the formula will return the value in G2.

And as Harry points out, the formula I provided is not robust and can give an incorrect value if your data is such that the lookup value in A2 can be in one of the cells you do not intend to lookup, ie. E, G, I, K, etc. If that can never be the case, the formula will work fine.

Not having seen your data, I assumed the formula would be appropriate for your data. IF it can be the case where the lookup value in A2 can be in one of the cells you do not intend to lookup, you will want to use the formula that Harry provided, as he has explained above.
 
Upvote 0
Indeed. It depends on the data. If the data is arranged in such a way that there couldn't possibly be a match where you don't want there to be a match then Joyner's formula will be faster than the array formula I posted. For example if A2 contains text, and D2, F2, H2 etc all contain text, but E2, G2, I2... etc contain numbers then Joyner's formula will be fine.
 
Upvote 0

Forum statistics

Threads
1,214,868
Messages
6,122,005
Members
449,059
Latest member
mtsheetz

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