Using AND OR with nested IF

Emile du Toit

New Member
Joined
Mar 7, 2015
Messages
22
[TABLE="width: 800"]
<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]
[/TR]
[TR]
[TD]ROW 2[/TD]
[TD]X[/TD]
[TD]19[/TD]
[TD][/TD]
[TD]23[/TD]
[TD]45[/TD]
[TD]18[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ROW 3[/TD]
[TD]Y[/TD]
[TD]99[/TD]
[TD]103[/TD]
[TD]100[/TD]
[TD]123[/TD]
[TD]88[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ROW 4[/TD]
[TD]Z[/TD]
[TD]987[/TD]
[TD]1001[/TD]
[TD]1020[/TD]
[TD]888[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I am trying to populate column H, using data from the bolded columns. What the formula is supposed to do is compare C2 and D2 and provided they are not both blank, it needs to use the larger of the two (C>=D or D>C). Let us call this value CD.
It is going to be comparing this value as a percentage of E2, with a few rules involved:
a. 0 < CD2 <= 20, then (CD2+5) / E2*100
b. 20 < CD2 <= 100, then (CD2 + 10) / E2*100
c. 100 < CD2, then (CD2 / E2)*100

The formula I have created produces values, but is giving incorrect values on occasions, so clearly I havn't quite tied it down.

=IF(AND(OR(20<c2<=100, 20<d2<="100),AND(C2">=D2)),(C2+10)/E2*100,IF(AND(OR(20<c2<=100, 20<d2<="100),AND(D2">C2)),(D2+10)/E2*100,IF(AND(OR(0<c2<=20, d2<="20),AND(C2">=D2)),(C2+5)/E2*100,IF(AND(OR(0<c2<=20, d2<="20),AND(D2">C2)),(D2+5)/E2*100,IF(AND(OR(C2>100, D2>100),AND(C2>=D2)),C2/E2*100,D2/E2*100)))))

Please can you help me plug the leaks!!!!!</c2<=20,></c2<=20,></c2<=100,></c2<=100,>
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this and let me know if it doesn't work

=IF(MAX(C2,D2)=MEDIAN(0,20,MAX(C2,D2)),(MAX(C2,D2)+5)/E2*100,IF(MAX(C2,D2)=MEDIAN(21,MAX(C2,D2),100),(MAX(C2,D2)+10)/E2*100,MAX(C2,D2)/E2*100))
 
Upvote 0
Thanks snjpverna! It appears to be spitting out only correct values! Although ukmikeb has a very succinct formula, but for various reasons connected to updating I particularly do not want to use array formulas.

I have two questions - that are just connected to my own ignorance. I am trying to understand what your formula implies in terms of the changeover of values.
Firstly, does your formula mean that a value of exactly 100 gets the formula '+10/E2' or '/E2'
Secondly, what happens to values from 20 to 21?
 
Upvote 0
Very neat solution. Thank you. In my particular case though I want to avoid array formulas.

If you are referring to my solution it certainly is NOT an array formula (ie a formula that requires entry with Control-Shift-Enter)!

But there are arrays in there to return the required values which could quite easily be substituted a 2-column matrix on the worksheet.

And the values can be adjusted accordingly say 20.001 and 100.001.

hth

@snjpverma thanks for your comment in post #5
 
Upvote 0
Apologies. it just demonstrates my ignorance. I thought that was what those brackets denoted.
So with your formula
=(MAX(C2,D2)+LOOKUP(MAX(C2,D2),{0,21,101},{5,10,0}))/E2*100
I could just tweak it to
=(MAX(C2,D2)+LOOKUP(MAX(C2,D2),{0,20.000001,100.000001},{5,10,0}))/E2*100
And this would mean 0-20 (+5), 20.000001 - 100 (+10), 100.000001 and up (+0) ?
Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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