API reference · Finance

Mean

Returns the arithmetic mean of numeric values in a vector or the first column of a 2D array.

FunctionFinance→DoubleSource L110

Signature#

VBA
Function Mean(ByVal vArr As Variant) As Double

Parameters#

ParameterVBA typeDefaultDescription
vArrVariantByValRequiredInput 1D array or 2D array. For 2D input, only the column at its second-dimension lower bound is used.

Returns#

Double. Sum(vArr) divided by Count(vArr, True), or 0 if there are no qualifying numeric values or the input shape is rejected.

Example#

SEE THE TRANSFORMATIONAverage the first column only
Before
values2D · 3 × 2

Rows 1..3 · Columns 1..2

row / col12
11009000
2“200”9000
3Empty9000
After
Return valueValues / state

No array dimensions

ItemValue
Mean150

(100 + 200) ÷ 2 = 150. The second column is ignored; Empty does not add to the count.

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 ExampleFinanceMean()
    Dim f As New Finance
    Dim values As Variant
    ReDim values(1 To 3, 1 To 2)
    values(1, 1) = 100
    values(2, 1) = "200"
    values(3, 1) = Empty
    values(1, 2) = 9000
    values(2, 2) = 9000
    values(3, 2) = 9000
    Debug.Print f.Mean(values) '150; second column is ignored
End Sub

Behavior and edge cases#

Performance#

View implementation
VBA
Function Mean(ByVal vArr As Variant) As Double
    'Returns the mean of numeric values in a column vector

    Dim n                       As Long

    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

    n = Me.Count(vArr, True)
    If n = 0 Then Exit Function

    Mean = Me.Sum(vArr) / n
End Function