Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2013, 08:38 AM
BobBridges's Avatar
BobBridges BobBridges is offline Multiple If statements, Windows 7 64bit Multiple If statements, Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

Jamal, let me try. One thing I see wrong in your attachment is the way you're attempting to do "and" conditions:
Code:
=IF((A2>0&B2>0),A2+B2, 0)
In an Excel formula, "&" is the string-concatenation symbol, not the logical AND operator. So what does Excel see here? I've done a little testing and I think it's this:
Code:
(A2 > (0 & B2)) > 0
That is, first it concatenates 0 to B2. In your sample worksheet that yields the string "011". Then it checks to see whether A2 (which has the value 1) is greater than "011". Excel decides this is FALSE. Lastly it decides whether the result FALSE is greater than 0, which is TRUE. That's why you're always getting A+B in your worksheet.

To do an AND condition, change your formula to do this:

Code:
=IF(AND(A2>0,B2>0),A2+B2,0)
Maybe that's all you need; if you now know how to proceed, you're on your way. But if you're still having trouble, here are some comments:

1) You tried three time to explain to Pecoflyer what you want to accomplish, and although the explanations look clear enough to me, I do notice that you said it differently each time. The first time, on 07-28, you said:
A>0, B>0 => A+B
A>0, B<0 => A-B
A<0, B>0 => 2A
A<0, B<0 => 3B-9

The next day you changed what happened when A is negative:
A<0, B>0 => =2A-B
A<0, B<0 => 3B-A

The third time, 07-30, you just gave constants:
A>0, B>0 => 10
A>0, B<0 => 20
A<0, B>0 => 30
A<0, B<0 => 40

I think I know why you did that: You already know how to do the arithmetic calculation, but you wanted to ask only about the IF formula. But if there's another reason, what is it?

2) If you're not sure how to combine many IF statements together, here's one sample. It uses your first set of calculations, and if any of the values in A or B is equal to 0 (rather than greater or less) then it displays a blank value:
Code:
=IF(AND($A2>0,$B2>0),$A2+$B2,IF(AND($A2>0,$B2<0),$A2-$B2,IF(AND($A2<0,$B2>0),$A2*2,IF(AND($A2<0,$B2<0),3*$B2-9,""))))
That's doing it the longest possible way. If you know that none of your values can ever be equal to 0, you can make it shorter:
Code:
=IF($A2>0,IF($B2>0,$A2+$B2,$A2-$B2),IF($B2>0,$A2*2,$B2*3-9))
If you like being clever—and I do—you can do it a little differently. Instead of doing multiple IFs and ANDs, consider turning the four possible combinations into choices 1, 2, 3 and 4. For this the calculation is
Code:
=(1-SIGN(A2))+(1-SIGN(B2))/2+1
What good is that?, you ask? Well, now you can tell Excel to CHOOSE which of four formula to apply:
Code:
=CHOOSE((1-SIGN(A2))+(1-SIGN(B2))/2+1,A2+B2,A2-B2,A2*2,B2*3-9)
The index calculation is harder to read than the IF and AND formulae, but the final calculations are lined up and maybe it's easier to pick them out this way.

Feel free to ask more questions, but I hope this helps out some.
Reply With Quote
  #2  
Old 08-07-2013, 01:41 AM
Jamal NUMAN Jamal NUMAN is offline Multiple If statements, Windows 7 64bit Multiple If statements, Office 2010 64bit
Expert
Multiple If statements,
 
Join Date: Nov 2010
Posts: 527
Jamal NUMAN is on a distinguished road
Thumbs up

Quote:
Originally Posted by BobBridges View Post
Jamal, let me try. One thing I see wrong in your attachment is the way you're attempting to do "and" conditions:
Code:
=IF((A2>0&B2>0),A2+B2, 0)
In an Excel formula, "&" is the string-concatenation symbol, not the logical AND operator. So what does Excel see here? I've done a little testing and I think it's this:
Code:
(A2 > (0 & B2)) > 0
That is, first it concatenates 0 to B2. In your sample worksheet that yields the string "011". Then it checks to see whether A2 (which has the value 1) is greater than "011". Excel decides this is FALSE. Lastly it decides whether the result FALSE is greater than 0, which is TRUE. That's why you're always getting A+B in your worksheet.

To do an AND condition, change your formula to do this:

Code:
=IF(AND(A2>0,B2>0),A2+B2,0)
Maybe that's all you need; if you now know how to proceed, you're on your way. But if you're still having trouble, here are some comments:

1) You tried three time to explain to Pecoflyer what you want to accomplish, and although the explanations look clear enough to me, I do notice that you said it differently each time. The first time, on 07-28, you said:
A>0, B>0 => A+B
A>0, B<0 => A-B
A<0, B>0 => 2A
A<0, B<0 => 3B-9

The next day you changed what happened when A is negative:
A<0, B>0 => =2A-B
A<0, B<0 => 3B-A

The third time, 07-30, you just gave constants:
A>0, B>0 => 10
A>0, B<0 => 20
A<0, B>0 => 30
A<0, B<0 => 40

I think I know why you did that: You already know how to do the arithmetic calculation, but you wanted to ask only about the IF formula. But if there's another reason, what is it?

2) If you're not sure how to combine many IF statements together, here's one sample. It uses your first set of calculations, and if any of the values in A or B is equal to 0 (rather than greater or less) then it displays a blank value:
Code:
=IF(AND($A2>0,$B2>0),$A2+$B2,IF(AND($A2>0,$B2<0),$A2-$B2,IF(AND($A2<0,$B2>0),$A2*2,IF(AND($A2<0,$B2<0),3*$B2-9,""))))
That's doing it the longest possible way. If you know that none of your values can ever be equal to 0, you can make it shorter:
Code:
=IF($A2>0,IF($B2>0,$A2+$B2,$A2-$B2),IF($B2>0,$A2*2,$B2*3-9))
If you like being clever—and I do—you can do it a little differently. Instead of doing multiple IFs and ANDs, consider turning the four possible combinations into choices 1, 2, 3 and 4. For this the calculation is
Code:
=(1-SIGN(A2))+(1-SIGN(B2))/2+1
What good is that?, you ask? Well, now you can tell Excel to CHOOSE which of four formula to apply:
Code:
=CHOOSE((1-SIGN(A2))+(1-SIGN(B2))/2+1,A2+B2,A2-B2,A2*2,B2*3-9)
The index calculation is harder to read than the IF and AND formulae, but the final calculations are lined up and maybe it's easier to pick them out this way.

Feel free to ask more questions, but I hope this helps out some.
Many thanks BobBridges for the massive help,

Correct. You got my point. All what I wanted to know is how to write an If statement for the cases below

if A3>0 and B3>0 then C3=10
If A3>0 and B3<0 then C3=20
If A3<0 and B3>0 then C3=30
If A3<0 and B3<0 then C3=40


The equation you have provided works fine (attached)

=IF(AND(A3>0,B3>0),10,IF(AND(A3>0,B3<0),20,IF(AND( A3<0,B3>0),30,IF(AND(A3<0,B3<0),40,""))))


Best

Jamal
Attached Images
File Type: jpg Clip_542.jpg (74.3 KB, 41 views)
__________________
Jamal NUMAN, Jamal432@gmail.com, P.O.BoX: 731, Ramallah, West Bank.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple If statements, Please tell me it doesn't take 7 statements to turn table borders off Jennifer Murphy Word VBA 5 07-25-2012 03:12 PM
Multiple If statements, showing results of If statements in a mail merge m&co Mail Merge 3 01-06-2012 07:27 PM
Multiple Input, Multiple Digital Signatures Jeff Word 0 01-04-2012 09:03 AM
Multiple task lists and multiple calendars kballing Outlook 0 01-18-2011 10:23 AM
IF statements that shifts to right. kent Excel 0 01-19-2006 02:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:27 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft