Search code examples
vbaexcelmaxworksheet-function

Application.Worksheetfunction.Max does not return the largest number in array


I have an array filled with various double values, when I use the Application.WorksheetFunction.max Function I receive one value yet when I look at the individual values of the array I can find multiple values that are larger the value returned by Application.WorksheetFunction.max. Copied form the immediate window:

?Application.WorksheetFunction.max(dummyQuantileVector)
 21081,4351851852 
?dummyQuantileVector(73368)
 799470,509259259 

What could be the cause of this?

Update: When I write the entire dummyQuantileVector array into a sheet and then use the use the max Function there I get the correct value.


Solution

  • It appears that WorksheetFunction.Max will only work on the first 34465 elements of the array of a 1D array. It will work properly on a 2D array.

    enter image description here

    Custom Max Function

    Function getMaxNumberFromArray(Data As Variant)
        Dim v As Variant
        Dim n As Double
        Dim flag As Boolean
    
        For Each v In Data
            If Not flag Then
                n = v
                flag = True
            ElseIf v > n Then
                n = v
            End If
        Next
        getMaxNumberFromArray = n
    End Function