API reference · Arrays
DimSplit
Convert a 2D matrix into a 1D outer array containing one 1D array per row.
Signature#
Function DimSplit(ByVal vArr As Variant) As VariantParameters#
| Parameter | VBA type | Default | Description |
|---|---|---|---|
vArr | VariantByVal | Required | Allocated two-dimensional array to split into row arrays. |
Returns#
Variant containing a 1D outer array with the source row bounds. Every inner 1D row array has the source column bounds. Returns Empty for non-arrays or unsupported rank.
Example#
Before
transactions2D · 2 × 2
Rows 1..2 · Columns 1..2
| row / col | 1 | 2 |
|---|---|---|
| 1 | “Sales” | 100 |
| 2 | “Fees” | 12 |
After
rows1D outer · (2,)
Outer indices 1..2 · each element is a 1D array
Element 11D · (2,)
| 1 | 2 |
|---|---|
| “Sales” | 100 |
Element 21D · (2,)
| 1 | 2 |
|---|---|
| “Fees” | 12 |
The outer array has rank 1. Its elements contain independent 1D row arrays, not a second rectangular dimension.
Shaded headers are array indices, not data. Quoted cells are text. Values shown are derived from the source, not an Excel execution.
Sub ExampleDimSplit()
Dim a As New Arrays
Dim transactions As Variant, rows As Variant
ReDim transactions(1 To 2, 1 To 2)
transactions(1, 1) = "Sales": transactions(1, 2) = 100
transactions(2, 1) = "Fees": transactions(2, 2) = 12
rows = a.DimSplit(transactions)
Debug.Print rows(1)(1), rows(1)(2) ' Expected: Sales, 100
Debug.Print a.DimCounter(rows) ' Expected: 1; inner arrays are separate
End SubBehavior and edge cases#
- The output uses nested indexing, such as rows(rowIndex)(columnIndex), rather than matrix indexing rows(rowIndex, columnIndex).
- Both sets of source bounds are preserved in the corresponding outer and inner arrays.
- Every row receives a row-array template and then all source elements are copied into it. Row order and value types are preserved.
- DimMerge reverses this representation when the inner arrays have compatible bounds. The original matrix is not modified.
Performance#
O(r × c) element work and O(r × c) output storage, plus an O(c) row template and O(r) outer-array descriptors.
View implementation
Function DimSplit(ByVal vArr As Variant) As Variant
'Converts a two-dimensional array into a one-dimensional array of row arrays
Dim vArr_Template As Variant
Dim lRow As Long
Dim lCol As Long
Dim vArr_Out As Variant
If Not IsArray(vArr) Then Exit Function
If Me.DimCounter(vArr) <> 2 Then Exit Function
ReDim vArr_Out(LBound(vArr, 1) To UBound(vArr, 1))
ReDim vArr_Template(LBound(vArr, 2) To UBound(vArr, 2))
For lRow = LBound(vArr_Out) To UBound(vArr_Out)
vArr_Out(lRow) = vArr_Template
For lCol = LBound(vArr, 2) To UBound(vArr, 2)
vArr_Out(lRow)(lCol) = vArr(lRow, lCol)
Next lCol
Next lRow
If IsArray(vArr_Out) Then
DimSplit = vArr_Out
End If
End Function