API reference · Arrays
DimExpand
Convert a 1D array into a 2D column array.
Signature#
Function DimExpand(ByVal vArr As Variant) As VariantParameters#
| Parameter | VBA type | Default | Description |
|---|---|---|---|
vArr | VariantByVal | Required | Allocated 1D array. Each element becomes one row in output column 1. |
Returns#
Variant containing a 2D array bounded (original element bounds, 1 To 1). Returns Empty for non-arrays and inputs that DimCounter does not identify as rank one.
Example#
Before
amounts1D · (3,)
Indices 0..2 · one dimension
| 0 | 1 | 2 |
|---|---|---|
| 120 | 80 | 50 |
After
amountColumn2D · 3 × 1
Rows 0..2 · Columns 1..1
| row / col | 1 |
|---|---|
| 0 | 120 |
| 1 | 80 |
| 2 | 50 |
This shows the first DimExpand result. The example then rebases rows to 1..3 using Change_Lbound.
Shaded headers are array indices, not data. Quoted cells are text. Values shown are derived from the source, not an Excel execution.
Sub ExampleDimExpand()
Dim a As New Arrays
Dim amounts As Variant, amountColumn As Variant
amounts = VBA.Array(120, 80, 50)
amountColumn = a.DimExpand(amounts)
Debug.Print LBound(amountColumn, 1) ' Expected: 0
Debug.Print amountColumn(2, 1) ' Expected: 50
' To create a one-based column for an Excel range:
amountColumn = a.Change_Lbound(amountColumn, 1, 1)
Debug.Print amountColumn(1, 1) ' Expected: 120
End SubBehavior and edge cases#
- The source 1D bounds become the output row bounds. The output column always has lower and upper bound 1.
- This is a column expansion, not a row expansion. Use Transpose on the result to obtain a 2D row.
- The function does not recursively expand nested arrays: an outer 1D array of row arrays becomes a column whose elements still contain arrays.
- Values are copied without filtering or conversion. The input array is not modified.
Performance#
O(n) time and O(n) output storage for n source elements.
View implementation
Function DimExpand(ByVal vArr As Variant) As Variant
'Converts a one-dimensional array into a two-dimensional column array
Dim vArr_Out As Variant
Dim iRow As Long
Dim iCol As Long
Dim iDim As Long
If Not IsArray(vArr) Then Exit Function
If Me.DimCounter(vArr) <> 1 Then Exit Function
ReDim vArr_Out(LBound(vArr, 1) To UBound(vArr, 1), 1 To 1)
iCol = UBound(vArr, 1)
For iRow = LBound(vArr_Out, 1) To UBound(vArr_Out, 1)
vArr_Out(iRow, 1) = vArr(iRow)
Next iRow
If IsArray(vArr_Out) Then
DimExpand = vArr_Out
End If
End Function