Combine multiple IF functions

gomes123

New Member
Joined
Jun 16, 2021
Messages
35
Office Version
  1. 2007
Platform
  1. Windows
Is there a way to combine these 4 IF functions into 1 function?

VBA Code:
=IF(AND(E3>C3,G3>E3,F3>D3,H3>F3),"UP","NO")
=IF(AND(C3>E3,E3>G3,D3>F3,F3>H3),"DOWN","NO")
=IF(AND(E3>C3,G3>E3,D3>F3,F3>H3),"LEFT","NO")
=IF(AND(C3>E3,E3>G3,F3>D3,H3>F3),"RIGHT","NO")

The most I could do is write it like
VBA Code:
=IF(OR(AND(E3>C3,G3>E3,F3>D3,H3>F3),AND(C3>E3,E3>G3,D3>F3,F3>H3),AND(E3>C3,G3>E3,D3>F3,F3>H3),AND(C3>E3,E3>G3,F3>D3,H3>F3)),"YES","NO")

But the problem I need it to state specificially (UP/DOWN/LEFT/RIGHT) but the code above could only output values values is true/false (YES/NO).

Thanks!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try:

Excel Formula:
=IF(AND(E3>C3,G3>E3,F3>D3,H3>F3),"UP",IF(AND(C3>E3,E3>G3,D3>F3,F3>H3),"DOWN",IF(AND(E3>C3,G3>E3,D3>F3,F3>H3),"LEFT",IF(AND(C3>E3,E3>G3,F3>D3,H3>F3),"RIGHT","NO"))))

Or a little more succinctly, and easier to extend ....

Excel Formula:
=IFERROR(INDEX({"Up","Down","Left","Right"},MATCH(SIGN(E3-C3)*1000+SIGN(G3-E3)*100+SIGN(F3-D3)*10+SIGN(H3-F3),{1111,-1111,1089,-1089},)),"No")
 
Last edited:
Upvote 1
Solution
Try:

Excel Formula:
=IF(AND(E3>C3,G3>E3,F3>D3,H3>F3),"UP",IF(AND(C3>E3,E3>G3,D3>F3,F3>H3),"DOWN",IF(AND(E3>C3,G3>E3,D3>F3,F3>H3),"LEFT",IF(AND(C3>E3,E3>G3,F3>D3,H3>F3),"RIGHT","NO"))))

Or a little more succinctly, and easier to extend ....

Excel Formula:
=IFERROR(INDEX({"Up","Down","Left","Right"},MATCH(SIGN(E3-C3)*1000+SIGN(G3-E3)*100+SIGN(F3-D3)*10+SIGN(H3-F3),{1111,-1111,1089,-1089},)),"No")

Thanks! It works perfectly!

I like your 2nd code (IFERROR), never thought of it that way, very elegant! Could you care to explain what the digits 1111,-1111,1089,-1089 mean, and how does it work? Does it matter if its 1111, and not say 2222 etc?
 
Upvote 0
Could you care to explain what the digits 1111,-1111,1089,-1089 mean, and how does it work?
For two numbers a and b, you are interested in the two results b>a and a>b, i.e. SIGN(b-a) =1 or -1.

For the four sets of two numbers, the results you're interested in are:
ABCDE
1Sign 1Sign 2Sign 3Sign 4Result
21111Up
3-1-1-1-1Down
411-1-1Left
5-1-111Right
Sheet1

To make the comparison easier, we can multiply by powers of ten to make a single number:
ABCDEF
1Sign 1Sign 2Sign 3Sign 4Result
21111Up
3-1-1-1-1Down
411-1-1Left
5-1-111Right
6
71000100101Up1111
8Down-1111
9Left1089
10Right-1089
Sheet1
Cell Formulas
RangeFormula
F7:F10F7=SUMPRODUCT(A2:D2,A$7:D$7)
 
Upvote 1
can multiply by powers of ten to make a single number:

Thanks for the explanation! Very intuitive and good way to go about it.

How did you come about to think of "multiplying it by powers of 10"? Is this a "best practices" method that is commonly used when using SUMPRODUCT or SIGN?
 
Upvote 0
How did you come about to think of "multiplying it by powers of 10"?
Using powers of some number N (not necessarily 10) is a technique that sometimes comes in handy in Excel formulae.

I wouldn't describe it as best practice, or something particular to SUMPRODUCT or SIGN, rather it's just a technique that is sometimes useful.
 
Upvote 1
Using powers of some number N (not necessarily 10) is a technique that sometimes comes in handy in Excel formulae.

I wouldn't describe it as best practice, or something particular to SUMPRODUCT or SIGN, rather it's just a technique that is sometimes useful.

Thanks for the explanation, that's a really useful technique that I've learnt now, most appreciated! I like the innovative way of thinking!
 
Upvote 0

Forum statistics

Threads
1,224,259
Messages
6,177,480
Members
452,782
Latest member
ZCapitao

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