Function for minimum non-zero with multiple additional conditions 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 &lt; 0 Or ValTwo &lt; 0 Then
        SelValue = -1
    ElseIf ValOne = 0 And ValTwo = 0 Then
        SelValue = 0
    ElseIf NeedMax Then
        If ValOne &gt; ValTwo _
            Then SelValue = ValOne _
            Else SelValue = ValTwo
    Else
        If ValOne = 0 Then
            SelValue = ValTwo
        ElseIf ValOne &gt; ValTwo And ValTwo &lt;&gt; 0 Then
            SelValue = ValTwo
        Else
            SelValue = ValOne
        End If
    End If

End Function


Can anyone suggest a more direct, and hopefully faster, approach?
Try to use meaningful names. What's names (SelValue, ValOne,...) stands for? It don't give info on their purpose or usage.

Furthermore, you can simplify a lot your conditional branchments, using only this:

If ValOne &lt; 0 Or ValTwo &lt; 0 Then
    SelValue = -1
' remove this following condition to discard case 4
Else If ValOne = 0 Or ValTwo = 0 Then
    SelValue = ValOne + ValTwo  
ElseIf ValOne &gt; ValTwo &lt;&gt; NeedMax Then  
    SelValue = ValTwo
Else
    SelValue = ValOne
End If


not fully tested, but should do the trick

Edit : fixed case (4), where NeedMax is false and one of values is 0.