API reference · Finance

Extremum

Finds the minimum or maximum numeric value in a vector or the first column of a 2D array.

FunctionFinance→DoubleSource L167

Signature#

VBA
Function Extremum(ByVal vArr As Variant, ByVal eExtremum As ExtremumType) As Double

Parameters#

ParameterVBA typeDefaultDescription
vArrVariantByValRequiredInput 1D array or 2D array. Only the first column by lower bound is inspected for 2D input.
eExtremumExtremumTypeByValRequiredFinance.ExtremumType value: eMin = 0 selects the minimum, eMax = 1 selects the maximum.

Returns#

Double. The selected numeric extremum. Returns 0 when no numeric value qualifies, the input shape is rejected, or eExtremum is neither eMin nor eMax.

Example#

SEE THE TRANSFORMATIONFind minimum and maximum
Before
values1D · (6,)

Indices 0..5 · one dimension

012345
12“-3”EmptyNull#N/A7
After
Return valuesValues / state

No array dimensions

ItemValue
Minimum · eMin = 0-3
Maximum · eMax = 112

Numeric text “-3” participates in the comparison. Missing and error values are skipped.

Shaded headers are array indices, not data. Quoted cells are text. Values shown are derived from the source, not an Excel execution.

VBA
Option Explicit

Public Sub ExampleFinanceExtremum()
    Dim f As New Finance
    Dim values As Variant
    values = VBA.Array(12, "-3", Empty, Null, CVErr(xlErrNA), 7)
    Debug.Print f.Extremum(values, 0) '-3; Finance.ExtremumType.eMin = 0
    Debug.Print f.Extremum(values, 1) '12; Finance.ExtremumType.eMax = 1
End Sub

Behavior and edge cases#

Performance#

O(n) time for n rows in the first column, with O(1) accumulator state. The 1D-to-2D normalization allocates O(n) elements; passing a Variant array ByVal can also require an array copy.

View implementation
VBA
Function Extremum(ByVal vArr As Variant, ByVal eExtremum As ExtremumType) As Double
    'Returns the minimum or maximum numeric value in a column vector
    'eMax = Max
    'eMin = Min
    
    Dim lRow                    As Long
    Dim lCol                    As Long
    Dim vValue                  As Variant
    Dim dblExtremum             As Double
    Dim dblValue                As Double
    Dim bFound                  As Boolean

    If Not IsArray(vArr) Then Exit Function
    If Arr.DimCounter(vArr) = 1 Then vArr = Arr.DimExpand(vArr)
    If Arr.DimCounter(vArr) <> 2 Then Exit Function
    If eExtremum <> eMin And eExtremum <> eMax Then Exit Function

    lCol = LBound(vArr, 2)

    For lRow = LBound(vArr, 1) To UBound(vArr, 1)
        vValue = vArr(lRow, lCol)
        If Not IsError(vValue) Then
            If Not IsNull(vValue) Then
                If Not IsEmpty(vValue) Then
                    If IsNumeric(vValue) Then
                        dblValue = CDbl(vValue)
                        If Not bFound Then
                            dblExtremum = dblValue
                            bFound = True
                        ElseIf eExtremum = eMax Then
                            If dblValue > dblExtremum Then dblExtremum = dblValue
                        Else
                            If dblValue < dblExtremum Then dblExtremum = dblValue
                        End If
                    End If
                End If
            End If
        End If
    Next lRow

    If bFound Then Extremum = dblExtremum
End Function