Results 1 to 5 of 5
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: Function lngMax(lng1 As Long, lng2 As Long)

    I wrote this a while ago, presumably because I couldn't find a MAX function in Word97SR2/VBA, or because a supplied function didn't deal with LONG arguments.

    I can't find a suitable reference to MAX in the HELP files for VBA.



    <pre>Public Function lngMax(lng1 As Long, lng2 As Long) As Long
    ' Procedure: lngMax
    ' Description: Return the maximum of two LONG values.
    ' By: Chris Greaves Inc.
    ' Inputs: LONG arguments.
    ' Returns: None
    ' Assumes: None.
    ' Side Effects: None..
    ' Tested: By the calls shown below.
    If lng1 > lng2 Then
    lngMax = lng1
    Else
    lngMax = lng2
    End If
    'Sub TESTlngMax()
    'MsgBox lngMax(1, 2) ' 2
    'MsgBox lngMax(1.3, 2.3) ' 2 (returns LONG)
    'MsgBox lngMax(0, 0) ' 0
    'MsgBox lngMax(1, -2) '1
    'MsgBox lngMax(-1, 2) '2
    'End Sub
    End Function
    </pre>


  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Code: Function lngMax(lng1 As Long, lng2 As Long)

    Try this. It will return the max from a list of values. You probably don't find MAX because it's a SQL keyword.

    <pre>Public Function ReturnMax(ParamArray varValueList() _
    As Variant)
    'Created by Charlotte Foust 6/20/2000
    'Returns the maximum value from the list
    'of values passed
    Dim intLoop As Integer
    Dim varMaxValue As Variant
    Dim varTempValue As Variant

    'loop through the values
    'and compare pairs of them
    For intLoop = 1 To UBound(varValueList)
    'assign a value to the variable
    varTempValue = varValueList(intLoop)
    'The first time through varMaxValue
    'will be empty, so no comparison is needed.
    If IsEmpty(varMaxValue) Then
    varMaxValue = varTempValue
    'If the new value is greater than
    'the old Max value, save the new one.
    ElseIf varTempValue > varMaxValue Then
    varMaxValue = varTempValue
    End If 'IsEmpty(varMaxValue)
    Next intLoop '= 1 To UBound(varValueList)
    'Return the Max value
    ReturnMax = varMaxValue
    End Function 'ReturnMax(ParamArray varValueList() _
    As Variant)</pre>

    Charlotte

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function lngMax(lng1 As Long, lng2 As Long)

    >You probably don't find MAX because it's a SQL keyword.

    But more than that, the actual keyword MAX is available but only, as the help file says to "Specify the maximum and minimum acceptable values for the Value property of a ScrollBar or SpinButton.". And you only get to see THAT if you actually type "MsgBox MAX(7,8)" and then F1 to the MAX.

    Now, your code finds the maximum value in an ARRAY, right? There still appears (to me) to be no built-in-function to return the maximum of two values.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Code: Function lngMax(lng1 As Long, lng2 As Long)

    Why would you need the MAX of two values? It has to be one or the other. If one is greater than the other, it's the Max. Otherthise, the other one is. BTW, My function works perfectly well with two values also; and it's a paramarray, not a regular array, so all you have to do is pass in a comma delimited list of values.

    As for MAX, what you're referring to is a property of certain controls. What I'm referring to is an aggregate function.
    Charlotte

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function lngMax(lng1 As Long, lng2 As Long)

    >Why would you need the MAX of two values?

    Perhaps because I grew up with FORTRAN?

    I suspect the real reason is that MAX is easier for the eye/brain to read and comprehend than yet another level of If statemenst.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •