This function works, but seems unnecessarily complicated. I would like to simplify but performance is vital as it is called many times.
The conditions are:
1) If either value is negative, then result is -1
2) If both values are zero, then result is zero
3) If NeedMax is true, result is largest value
4) If NeedMax is false, result is smallest non-zero value
CODE:
Public Function SelValue(ValOne As Long, ValTwo As Long, _
Optional NeedMax As Boolean = True) As Long
If ValOne < 0 Or ValTwo < 0 Then
SelValue = -1
ElseIf ValOne = 0 And ValTwo = 0 Then
SelValue = 0
ElseIf NeedMax Then
If ValOne > ValTwo _
Then SelValue = ValOne _
Else SelValue = ValTwo
Else
If ValOne = 0 Then
SelValue = ValTwo
ElseIf ValOne > ValTwo And ValTwo <> 0 Then
SelValue = ValTwo
Else
SelValue = ValOne
End If
End If
End Function
Can anyone suggest a more direct, and hopefully faster, approach?
SelValue
is returned as-1
I can check for that. It is called to find an index number for a find result. \$\endgroup\$