user13623188
user13623188

Reputation:

Excel: nested if with and/or

I would like to 'translate' in excel the following condition:

if((A<0 & B<0) OR (B>= 0 & A >=0), ABS(B-A),A-B)

I have tried as follows:

if(or(and(A1<0, B1<0),(B1>=0, A1 >=0), ABS(B1-A1), A1-B1)

but it does not work.

Do you know why?

You can imagine the following as dataset

A   B

-0.2  -0.3
0.2   0.1
0.1   -0.2

Upvotes: 1

Views: 78

Answers (2)

JvdV
JvdV

Reputation: 75960

Little alternative:

=IF(ISEVEN(COUNTIF(A1:B1,"<0")),ABS(B1-A1),A1-B1)

Or rather:

=IF(COUNTIF(A1:B1,"<0")=1,A1-B1,ABS(B1-A1))

Upvotes: 1

BigBen
BigBen

Reputation: 50162

You're missing an AND:

=IF(OR(AND(A1<0,B1<0),AND(A1>=0,B1>=0)),ABS(B1-A1),A1-B1)

enter image description here

Upvotes: 1

Related Questions