API reference · Arrays

DimExpand

Convert a 1D array into a 2D column array.

FunctionArrays→VariantSource L283

Signature#

VBA
Function DimExpand(ByVal vArr As Variant) As Variant

Parameters#

ParameterVBA typeDefaultDescription
vArrVariantByValRequiredAllocated 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#

SEE THE TRANSFORMATIONExpand a vector into a column
Before
amounts1D · (3,)

Indices 0..2 · one dimension

012
1208050
After
amountColumn2D · 3 × 1

Rows 0..2 · Columns 1..1

row / col1
0120
180
250

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.

VBA
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 Sub

Behavior and edge cases#

Performance#

O(n) time and O(n) output storage for n source elements.

View implementation
VBA
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