API reference · Finance

SquaredSum

Adds the squares of numeric values from a vector or the first column of a 2D array.

FunctionFinance→DoubleSource L44

Signature#

VBA
Function SquaredSum(ByVal vArr As Variant) As Double

Parameters#

ParameterVBA typeDefaultDescription
vArrVariantByValRequiredInput 1D array or 2D array. Only the first column by lower bound is read for 2D input.

Returns#

Double. The sum of x*x for accepted numeric values, or 0 when none qualify or the input shape is rejected.

Example#

SEE THE TRANSFORMATIONSquare, then sum
Before
values1D · (6,)

Indices 0..5 · one dimension

012345
34“5”EmptyNull#N/A
After
Return valueValues / state

No array dimensions

ItemValue
SquaredSum50

3² + 4² + 5² = 50. Numeric text is converted; 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 ExampleFinanceSquaredSum()
    Dim f As New Finance
    Dim values As Variant
    values = VBA.Array(3, 4, "5", Empty, Null, CVErr(xlErrNA))
    Debug.Print f.SquaredSum(values) '50 = 9 + 16 + 25
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 SquaredSum(ByVal vArr As Variant) As Double
    'Returns the sum of squared numeric values in a column vector

    Dim lRow                    As Long
    Dim lCol                    As Long
    Dim vValue                  As Variant
    Dim dblValue                As Double
    Dim dblSum                  As Double

    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

    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)
                        dblSum = dblSum + dblValue * dblValue
                    End If
                End If
            End If
        End If
    Next lRow

    SquaredSum = dblSum
End Function