IFNEGATIVE function

JeanM01

New Member
Joined
Jun 10, 2015
Messages
9
Hi everyone,

I am looking to write a VBA defined function that is similar to IFERROR(Action, what to do if error) instead mines would be
IFNEGATIVE( Action, what to do if negative). If I could create this, it would cut down on the number of IF's I have for a formula.


Thanks in advance,
Jean
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
If I could create this, it would cut down on the number of IF's I have for a formula.
Yes and No..

Yes, it would reduce the IF's in your formulas.
However, the IF would still exist, they're just being done by VBA instead of Formula.
So the same amount of work is being done, you've just delegated some of it to VBA instead of your formulas.
And VBA is NOT Faster than Formula.
UDF's actually tend to be slower than formulas.

But anyway, this will do what you're asking

Code:
Public Function IfNeg(c As Range, rslt As Variant)
If c.Value < 0 Then
    IfNeg = rslt
Else
    IfNeg = c.Value
End If
End Function

used like
=IFNEG(A1,"It's Negative")
 
Last edited:
Upvote 0
Welcome to the Board!

What formula are you using now? As for evaluating negative, you can use something like

If Target.Value < 0 Then

HTH,
 
Upvote 0
I am writing a code like this: If(Cell1*Cell2 + Cell3<0, Cell4, Cell1*Cell2+Cell3) It gets more complicated as I am doing a case basis.
 
Upvote 0
Probably should change
c As Range
to
c As Variant
Since your doing a calcuation in the formula, not just referencing a cell.
Rich (BB code):
Public Function IfNeg(c As Range, rslt As Variant)
If c.Value < 0 Then
    IfNeg = rslt
Else
    IfNeg = c.Value
End If
End Function
 
Upvote 0
ABCDEF
1ValueMeanSlope1Slope2InterceptEstimate
2100
3200
4300
54002502-35

<tbody>
</tbody>
So right now my formula for F5 is

IF(E5 + D5*A5+C5*A4<0, B2 ,E5 + D5*A5+C5*A4)

which seems like an awful amount to write versus IFNEGATIVE(E5 + D5*A5+C5*A4, B5)
 
Upvote 0
Not sure if this helps, but maybe...

Code:
Function IfNegative(s1 As String, s2 As String) As Variant
    Application.Volatile
    If Evaluate(s1) < 0 Then
        IfNegative = Evaluate(s2)
    Else
        IfNegative = Evaluate(s1)
    End If
End Function

Usage (enter the formulas as strings)
=IfNegative("=E5+D5*A5+C5*A4", "=B2")

Or if you want a message when negative
=IfNegative("=E5+D5*A5+C5*A4", """message""")

M.
 
Upvote 0

Forum statistics

Threads
1,215,332
Messages
6,124,313
Members
449,152
Latest member
PressEscape

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