API reference · Arrays

DimMerge

Convert a 1D array of compatible 1D row arrays into a 2D matrix.

FunctionArrays→VariantSource L328

Signature#

VBA
Function DimMerge(ByVal vArr As Variant) As Variant

Parameters#

ParameterVBA typeDefaultDescription
vArrVariantByValRequiredAllocated 1D outer array. Its first element must be a 1D array; every subsequent element must support all column subscripts found in that first row.

Returns#

Variant containing a 2D array with the outer array's bounds as rows and the first inner array's bounds as columns. Returns Empty when the outer value is not 1D or its first element is not a 1D array.

Example#

SEE THE TRANSFORMATIONMerge row vectors into a matrix
Before
rows1D outer · (2,)

Outer indices 0..1 · each element is a 1D array

Element 01D · (2,)
01
“Sales”100
Element 11D · (2,)
01
“Fees”12
After
transactions2D · 2 × 2

Rows 0..1 · Columns 0..1

row / col01
0“Sales”100
1“Fees”12

The outer bounds become row bounds; the first inner vector supplies column bounds.

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

VBA
Sub ExampleDimMerge()
    Dim a As New Arrays
    Dim rows As Variant, transactions As Variant

    rows = VBA.Array(VBA.Array("Sales", 100), _
                     VBA.Array("Fees", 12))
    transactions = a.DimMerge(rows)
    Debug.Print transactions(0, 0), transactions(1, 1) ' Expected: Sales, 12
    Debug.Print LBound(transactions, 1), LBound(transactions, 2) ' Expected: 0, 0
End Sub

Behavior and edge cases#

Performance#

O(r × c) time and O(r × c) output storage for r outer elements and c columns taken from the first inner row.

View implementation
VBA
Function DimMerge(ByVal vArr As Variant) As Variant
    'Converts an array of row arrays into a two-dimensional array

    Dim vArr_Out As Variant
    Dim lRow As Long
    Dim lCol As Long

    If Not IsArray(vArr) Then Exit Function
    If Me.DimCounter(vArr) <> 1 Then Exit Function
    If Not IsArray(vArr(LBound(vArr))) Then Exit Function
    If Me.DimCounter(vArr(LBound(vArr))) <> 1 Then Exit Function
    ReDim vArr_Out(LBound(vArr) To UBound(vArr), LBound(vArr(LBound(vArr))) To UBound(vArr(LBound(vArr))))
    For lRow = LBound(vArr_Out, 1) To UBound(vArr_Out, 1)
        For lCol = LBound(vArr_Out, 2) To UBound(vArr_Out, 2)
            vArr_Out(lRow, lCol) = vArr(lRow)(lCol)
        Next lCol
    Next lRow

    If IsArray(vArr_Out) Then
        DimMerge = vArr_Out
    End If
End Function