Access/VBA Library

Max

Returns the largest value in a set of values.

Syntax

Max( args-1 [, args-2 [,...[, args-N]]] )
Parameters
Name Type Description
args Variant Required. Values for which you want to determine the largest value. Arguments of type Object are ignored.
Return value

Variant : The largest value of the given set of values.

Code
Public Function Max(ParamArray args())
    Dim MaxValue
    Dim Index As Long
    
    MaxValue = Empty
    
    For Index = LBound(args) To UBound(args)
        If Not IsMissing(args(Index)) Then
            If Not IsObject(args(Index)) Then
                If args(Index) > MaxValue Then MaxValue = args(Index)
            End If
        End If
    Next Index
    
    Max = MaxValue
End Function

Remarks

  • It's wise to make sure the arguments are of the same type.
Example
  ' Comparing numeric values
  MsgBox Max(1, 4, 6.1, 5, -3, 4, 6)
  ' Will result in : 6.1
  '
  ' Comparing strings
  Msgbox Max("ABC", "Just a string", "Whatever", "what's new?")
  ' Will result in : what's new?
  '
  ' Comparing dates
  MsgBox Max(Date, Now(), DateAdd("m", 1, Date))
  ' Will result in the value of the @DateAdd funtion