API reference · Arrays

SliceArray

Extract a row or column from a 2D array while keeping a 2D result.

FunctionArrays→Variant (implicit)Source L83

Signature#

VBA
Function SliceArray(ByVal vArr As Variant, ByVal SelectedType As SelectionType, ByVal lIndex As Long)

Parameters#

ParameterVBA typeDefaultDescription
vArrVariantByValRequiredAllocated two-dimensional Variant array to read.
SelectedTypeSelectionTypeByValRequiredSelectionType.eSelectLine = 2 selects one row. SelectionType.eSelectColumn = 1 selects one column. These values differ from DeletionType's row/column mapping.
lIndexLongByValRequiredActual 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#

SEE THE TRANSFORMATIONSelect column 2
Before
transactions2D · 3 × 2

Rows 1..3 · Columns 1..2

row / col12
1“Sales”120
2“Fees”15
3“Sales”80
After
amounts2D · 3 × 1

Rows 1..3 · Columns 1..1

row / col1
1120
215
380

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.

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

Behavior and edge cases#

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