PDA

View Full Version : Solved: min of 2 nos, if positive



nitzbar
08-08-2008, 01:51 PM
Hi.... I need to find the minimum of 2 nos, weeding out zeros and negative numbers.... i tried this.. but it didn't work...

FUNCTION CALCULATING MIN OF 2 NUMBERS
Public Function minx(num1 As Double, num2 As Double) As Double



tmp1 = num1: temp2 = num2

If tmp1 > num2 Then tmp1 = num2: tmp2 = num1
If tmp1 >= 0 Then

k = tmp2
Else
k = tmp1

End If
minx = k

End Function

Mavyak
08-08-2008, 02:22 PM
What should be the return value if both numbes are less than or equal to zero?

Bob Phillips
08-08-2008, 02:23 PM
Public Function minx(num1 As Double, num2 As Double) As Variant

If num1 <= 0 Then

If num2 <= 0 Then

minx = CVErr(xlErrNum)
Else

minx = num2
End If

ElseIf num2 <= 0 Then

minx = num2

Else

minx = num1
If num2 < num1 Then minx = num2
End If

End Function

mikerickson
08-08-2008, 07:01 PM
This spreadsheet formula will do that.
=MIN(((A5<=0)*9E+99)+A5,((B5<=0)*9E+99)+B5)

nitzbar
08-11-2008, 11:28 AM
Thanks.... I've got it working....:yes