API reference · Arrays
FillColumn
Replace an existing column with values from a positional column vector.
Signature#
Function FillColumn(ByVal vArr As Variant, ByVal vColumn As Variant, ByVal lIndex As Long) As VariantParameters#
| Parameter | VBA type | Default | Description |
|---|---|---|---|
vArr | VariantByVal | Required | Allocated 1D or 2D destination array. A 1D destination is expanded to one column before replacement. |
vColumn | VariantByVal | Required | Allocated 1D vector or a 2D single-column array with exactly as many rows as the destination. Row bounds may differ. |
lIndex | LongByVal | Required | Actual 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#
Before
transactions2D · 2 × 2
Rows 1..2 · Columns 1..2
| row / col | 1 | 2 |
|---|---|---|
| 1 | “Invoice A” | 100 |
| 2 | “Invoice B” | 200 |
Additional input
corrected1D · (2,)
Indices 0..1 · one dimension
| 0 | 1 |
|---|---|
| 110 | 220 |
After
result2D · 2 × 2
Rows 1..2 · Columns 1..2
| row / col | 1 | 2 |
|---|---|---|
| 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.
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 SubBehavior and edge cases#
- This replaces an existing column. It does not add a new column; use InsertColumns to append one.
- 1D arguments are interpreted vertically by DimExpand. A 2D single-row replacement with several columns is rejected rather than treated as a vertical vector.
- Row counts must agree, but row lower bounds need not. Copying maps the first replacement row to the first destination row by positional offset.
- The destination's row and column bounds are preserved after any 1D expansion. All non-target columns retain their original values.
- The original destination and replacement arrays are not modified.
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
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