UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> GetMaxArrayValue    
CODE
' Code courtesy of UtterAccess Wiki
' http://www.utteraccess.com/wiki/index.php/Category:FunctionLibrary
' Original submission by John White (jwhite)
' Date contributed: 2010/08/27
'
'================================================================================
'NAME     : GetMaxArrayValue
'PURPOSE  : Returns the Maximum value of variables passed.  Alleviates
'           the need to compare variable values with lengthy code.
'RETURNS  : Variant
'ARGUMENTS: Two or more variables or values, separated by comma's
'REFERENCE: http://msdn.microsoft.com/en-us/library/ct363x9h%28VS.80%29.aspx
'
'USAGE    : In VBA Code:
'           intMax = GetMaxArrayValue(intScore1, intScore2, intScore3)
'
'           As Control Source for a Control:
'           =GetMaxArrayValue([txtScore1], [txtScore2], [txtScore3])
'--------------------------------------------------------------------------------

Function GetMaxArrayValue(ParamArray varValues()) As Variant
   On Error GoTo GetMaxArrayValue_Error

   Dim intCounter As Integer
   
   For intCounter = 0 To UBound(varValues)
       If GetMaxArrayValue < varValues(intCounter) Or IsNull(GetMaxArrayValue) Then
           GetMaxArrayValue = varValues(intCounter)
       End If
   Next

GetMaxArrayValue_Cleanup:
   Exit Function

GetMaxArrayValue_Error:
   'INSERT YOUR ERROR HANDLING CODE HERE
   Resume GetMaxArrayValue_Cleanup
End Function

The result of this function is not exact in this case : GetMaxArrayValue(Null,0) -> Null. I submit my function for this reason and because it is faster :

CODE

Public Function getMax(ParamArray pa() As Variant) As Variant
  Dim v As Variant
  For Each v In pa
     If getMax < v Then
        getMax = v
     ElseIf IsEmpty(getMax) And Not IsNull(v) Then
        getMax = v
     End If
  Next v
End Function

The same function to get the minimum :

CODE

Public Function getMin(ParamArray pa() As Variant) As Variant
  Dim v As Variant
  For Each v In pa
     If getMin > v Then
        getMin = v
     ElseIf IsEmpty(getMin) And Not IsNull(v) Then
        getMin = v
     End If
  Next v
End Function

Creative Commons License
GetMaxArrayValue by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 4,681 times.  This page was last modified 20:18, 19 March 2012 by Philben. Contributions by Jack Leach, Alan Greenwood and jwhite  Disclaimers