excel vba max function code example

Example: excel vba max function

'VBA function to find the maximum value from a list:

Function Max(ParamArray vals())
    Dim i&
    If UBound(vals) > -1 Then
        Max = vals(0)
        For i = 1 To UBound(vals)
            If vals(i) > Max Then Max = vals(i)
        Next
    End If
End Function

'------------------------------------------------------------------------------

MsgBox Max(7, 2, 0, 5.9, 8, 4.75)		'<--dsiplays: 8
    
    
'Note: the MAX() worksheet function is also available but requires the 
'slow call to the worksheet calcuation engine:
      
MsgBox WorksheetFunction.Max(7, 2, 0, 5.9, 8, 4.75)

Tags:

Html Example