Combine multiple IF functions

gomes123

New Member
Joined
Jun 16, 2021
Messages
31
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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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