IF statement (positive = positive ; negative = zero)

vanilladan

Board Regular
Joined
Sep 24, 2002
Messages
145
Hello folks,
I'm writing a lot of IF statements at the moments which involve
themes such as:

=IF(A1+B1<0,0,A1+B1)

only much longer.
The idea of these formulas is, as above, to say:

"If the result is positive, return that positive result.
If the result is negative, return a 0."

(It's a purchasing system, so you can't buy in negative stock, i.e. -12 apples).

What I want to know is, if there an easier way to do this
because each formula uses a duplicate statement, which is a pain in large formulas.

Any ideas?
cheers,

Vanilladan
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
This will do the same thing, not sure if this is what you are after (can you post some more detail)

Code:
=IF(SUM(A1:B1)<0,0,SUM(A1:B1))
 
Upvote 0
Jim,
Thanks for your reply.
The problem I've got - which remains in your formula - is that we're duplicating statements. Let's replace sum(A1+B1) [or whatever] with
"X". The statement says.

If (X>0,0,X)

We are using X twice: once as the criteria and once as the result.
Now imagine X is a statement like:

=ROUND((I2/H2)*100,0)+MOD(A2/(sum(b2*$A$5)-100))

And you can appreciate the problems I have...
 
Upvote 0
Try the following code to change all negative to zero :

Sub negativezeror()
For Each Cell In Range("A1:A20")
If Cell.Value <= 0 Then
Cell.Value = 0
Else
Cell.Value = Cell.Value
End If
Next
End Sub


Regards
 
Upvote 0
vanilladan said:
Jim,
Thanks for your reply.
The problem I've got - which remains in your formula - is that we're duplicating statements. Let's replace sum(A1+B1) [or whatever] with
"X". The statement says.

If (X>0,0,X)

We are using X twice: once as the criteria and once as the result.
Now imagine X is a statement like:

=ROUND((I2/H2)*100,0)+MOD(A2/(sum(b2*$A$5)-100))

And you can appreciate the problems I have...

Use MAX as suggested to you:

=MAX(0,ROUND((I2/H2)*100,0)+MOD(A2/((b2*$A$5)-100))
 
Upvote 0
Bat17,
Max was the perfect answer, thank you.
The simple solutions are always the best!

good work,

Vanilladan
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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