API reference · Arrays

FillColumn

Replace an existing column with values from a positional column vector.

FunctionArrays→VariantSource L807

Signature#

VBA
Function FillColumn(ByVal vArr As Variant, ByVal vColumn As Variant, ByVal lIndex As Long) As Variant

Parameters#

ParameterVBA typeDefaultDescription
vArrVariantByValRequiredAllocated 1D or 2D destination array. A 1D destination is expanded to one column before replacement.
vColumnVariantByValRequiredAllocated 1D vector or a 2D single-column array with exactly as many rows as the destination. Row bounds may differ.
lIndexLongByValRequiredActual destination-column subscript. For an expanded 1D destination, this must be 1.

Returns#

Variant containing a new 2D array with the expanded destination's bounds. Returns Empty for non-array arguments, unsupported rank, invalid column index, a multi-column replacement, or a row-count mismatch.

Example#

SEE THE TRANSFORMATIONReplace column 2
Before
transactions2D · 2 × 2

Rows 1..2 · Columns 1..2

row / col12
1“Invoice A”100
2“Invoice B”200
Additional input
corrected1D · (2,)

Indices 0..1 · one dimension

01
110220
After
result2D · 2 × 2

Rows 1..2 · Columns 1..2

row / col12
1“Invoice A”110
2“Invoice B”220

Values change; shape and bounds stay the same. The original matrix is unchanged.

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

VBA
Sub ExampleFillColumn()
    Dim a As New Arrays
    Dim transactions As Variant, corrected As Variant, result As Variant

    ReDim transactions(1 To 2, 1 To 2)
    transactions(1, 1) = "Invoice A": transactions(1, 2) = 100
    transactions(2, 1) = "Invoice B": transactions(2, 2) = 200
    corrected = VBA.Array(110, 220)
    result = a.FillColumn(transactions, corrected, 2)
    Debug.Print result(1, 1), result(1, 2) ' Expected: Invoice A, 110
    Debug.Print transactions(1, 2)       ' Expected: 100; source unchanged
End Sub

Behavior and edge cases#

Performance#

O(r × c) time and O(r × c) output storage for r rows and c columns, plus any O(r) temporary column expansions.

View implementation
VBA
Function FillColumn(ByVal vArr As Variant, ByVal vColumn As Variant, ByVal lIndex As Long) As Variant
    'Fills a specified column using a 1D or 2D column vector

    Dim vArr_Out                As Variant
    Dim lRow                    As Long
    Dim lCol                    As Long
    Dim lSourceRow              As Long
    Dim lSourceCol              As Long

    If Not IsArray(vArr) Or Not IsArray(vColumn) Then Exit Function
    If Me.DimCounter(vArr) = 1 Then vArr = Me.DimExpand(vArr)
    If Me.DimCounter(vArr) <> 2 Then Exit Function
    If Me.DimCounter(vColumn) = 1 Then vColumn = Me.DimExpand(vColumn)
    If Me.DimCounter(vColumn) <> 2 Then Exit Function

    If lIndex < LBound(vArr, 2) Or lIndex > UBound(vArr, 2) Then Exit Function
    If LBound(vColumn, 2) <> UBound(vColumn, 2) Then Exit Function
    If UBound(vColumn, 1) - LBound(vColumn, 1) <> UBound(vArr, 1) - LBound(vArr, 1) Then Exit Function

    ReDim vArr_Out(LBound(vArr, 1) To UBound(vArr, 1), LBound(vArr, 2) To UBound(vArr, 2))
    lSourceCol = LBound(vColumn, 2)

    For lRow = LBound(vArr, 1) To UBound(vArr, 1)
        lSourceRow = LBound(vColumn, 1) + lRow - LBound(vArr, 1)
        For lCol = LBound(vArr, 2) To UBound(vArr, 2)
            If lCol = lIndex Then
                vArr_Out(lRow, lCol) = vColumn(lSourceRow, lSourceCol)
            Else
                vArr_Out(lRow, lCol) = vArr(lRow, lCol)
            End If
        Next lCol
    Next lRow

    FillColumn = vArr_Out
End Function