API reference · Arrays
DimMerge
Convert a 1D array of compatible 1D row arrays into a 2D matrix.
Signature#
Function DimMerge(ByVal vArr As Variant) As VariantParameters#
| Parameter | VBA type | Default | Description |
|---|---|---|---|
vArr | VariantByVal | Required | Allocated 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#
Before
rows1D outer · (2,)
Outer indices 0..1 · each element is a 1D array
Element 01D · (2,)
| 0 | 1 |
|---|---|
| “Sales” | 100 |
Element 11D · (2,)
| 0 | 1 |
|---|---|
| “Fees” | 12 |
After
transactions2D · 2 × 2
Rows 0..1 · Columns 0..1
| row / col | 0 | 1 |
|---|---|---|
| 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.
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 SubBehavior and edge cases#
- Only the first inner array is checked for rank. Later rows are accessed directly and are not validated for type, rank or bounds.
- Ragged rows, scalar row elements and missing required subscripts can raise runtime errors. Extra columns in later rows are not copied because the first row determines the output column range.
- An empty outer array with no accessible first element can fail when the function reads vArr(LBound(vArr)).
- DimMerge is suitable for reversing DimSplit's rectangular output. Values are copied in outer-array order, and the source is not modified.
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
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