API reference · Arrays
SliceArray
Extract a row or column from a 2D array while keeping a 2D result.
Signature#
Function SliceArray(ByVal vArr As Variant, ByVal SelectedType As SelectionType, ByVal lIndex As Long)Parameters#
| Parameter | VBA type | Default | Description |
|---|---|---|---|
vArr | VariantByVal | Required | Allocated two-dimensional Variant array to read. |
SelectedType | SelectionTypeByVal | Required | SelectionType.eSelectLine = 2 selects one row. SelectionType.eSelectColumn = 1 selects one column. These values differ from DeletionType's row/column mapping. |
lIndex | LongByVal | Required | Actual source row or column subscript, including any nonstandard lower bound. The function does not validate this subscript. |
Returns#
Implicit Variant. A row slice has bounds (1 To 1, original column bounds). A column slice has bounds (original row bounds, 1 To 1). Returns Empty for non-arrays, non-2D arrays, or an unsupported selection value.
Example#
Before
transactions2D · 3 × 2
Rows 1..3 · Columns 1..2
| row / col | 1 | 2 |
|---|---|---|
| 1 | “Sales” | 120 |
| 2 | “Fees” | 15 |
| 3 | “Sales” | 80 |
After
amounts2D · 3 × 1
Rows 1..3 · Columns 1..1
| row / col | 1 |
|---|---|
| 1 | 120 |
| 2 | 15 |
| 3 | 80 |
Three rows remain. The selected column becomes column 1.
Shaded headers are array indices, not data. Quoted cells are text. Values shown are derived from the source, not an Excel execution.
Sub ExampleSliceArray()
Dim a As New Arrays
Dim transactions As Variant, amounts As Variant
ReDim transactions(1 To 3, 1 To 2)
transactions(1, 1) = "Sales": transactions(1, 2) = 120
transactions(2, 1) = "Fees": transactions(2, 2) = 15
transactions(3, 1) = "Sales": transactions(3, 2) = 80
' SelectionType.eSelectColumn = 1
amounts = a.SliceArray(transactions, 1, 2)
Debug.Print amounts(1, 1), amounts(3, 1) ' Expected: 120, 80
End SubBehavior and edge cases#
- Only true 2D arrays are accepted. A 1D array is not expanded automatically.
- The unselected axis keeps its source lower and upper bounds. The selected axis becomes a single element with subscript 1.
- An out-of-range lIndex reaches direct array access and raises a runtime subscript error. Validate it against LBound and UBound before calling.
- Values are copied in source order without comparisons or normalization. Empty, Null and error-valued elements are copied as values.
- The input array is not modified.
Performance#
O(c) time and output storage for a row slice, or O(r) for a column slice, where r and c are source rows and columns.
View implementation
Function SliceArray(ByVal vArr As Variant, ByVal SelectedType As SelectionType, ByVal lIndex As Long)
'Extracts a specified row or column from a two-dimensional array
Dim vArr_Out As Variant
Dim lDimResize As Long
Dim lSelect As Long
Dim lCol As Long
If Not IsArray(vArr) Then Exit Function
If Me.DimCounter(vArr) <> 2 Then Exit Function
Select Case SelectedType
Case eSelectLine
ReDim vArr_Out(1 To 1, LBound(vArr, 2) To UBound(vArr, 2))
For lSelect = LBound(vArr_Out, SelectedType) To UBound(vArr_Out, SelectedType)
vArr_Out(1, lSelect) = vArr(lIndex, lSelect)
Next lSelect
Case eSelectColumn
ReDim vArr_Out(LBound(vArr, SelectedType) To UBound(vArr, SelectedType), 1 To 1)
For lSelect = LBound(vArr, SelectedType) To UBound(vArr, SelectedType)
vArr_Out(lSelect, 1) = vArr(lSelect, lIndex)
Next lSelect
End Select
If IsArray(vArr_Out) And Not IsEmpty(vArr_Out) Then
SliceArray = vArr_Out
End If
End Function