API reference · Arrays

Change_Lbound

Rebase one dimension of a 1D or 2D array without changing its values or shape.

FunctionArrays→VariantSource L843

Signature#

VBA
Function Change_Lbound(ByVal vArr As Variant, ByVal lLbound As Long, ByVal lDim As Long) As Variant

Parameters#

ParameterVBA typeDefaultDescription
vArrVariantByValRequiredAllocated 1D or 2D array to copy with different index bounds.
lLboundLongByValRequiredNew lower bound for the selected dimension. The new upper bound is this value plus the original dimension length minus one.
lDimLongByValRequiredDimension to rebase: 1 for a 1D array; 1 for rows or 2 for columns of a 2D array. Other choices return Empty.

Returns#

Variant containing a new array of the same rank and lengths. The selected dimension starts at lLbound, and an unselected 2D dimension retains its bounds. Returns Empty for non-arrays, unsupported rank or an invalid dimension choice.

Example#

SEE THE TRANSFORMATIONRebase dimension 1 to index 1
Before
amounts1D · (3,)

Indices 0..2 · one dimension

012
1208050
After
oneBased1D · (3,)

Indices 1..3 · one dimension

123
1208050

The element count and values are unchanged. Only the indices move from 0..2 to 1..3.

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

VBA
Sub ExampleChangeLbound()
    Dim a As New Arrays
    Dim amounts As Variant, oneBased As Variant

    amounts = VBA.Array(120, 80, 50)
    oneBased = a.Change_Lbound(amounts, 1, 1)
    Debug.Print LBound(oneBased), UBound(oneBased) ' Expected: 1, 3
    Debug.Print oneBased(1), oneBased(3)         ' Expected: 120, 50
End Sub

Behavior and edge cases#

Performance#

O(n) time and output storage for a 1D array, or O(r × c) for a 2D array.

View implementation
VBA
Function Change_Lbound(ByVal vArr As Variant, ByVal lLbound As Long, ByVal lDim As Long) As Variant
    'Changes the lower bound of a selected dimension without changing its values

    Dim vArr_Out                As Variant
    Dim lRow                    As Long
    Dim lCol                    As Long
    Dim lRowSource              As Long
    Dim lColSource              As Long
    Dim lLast                   As Long
    Dim iDim                    As Integer

    If Not IsArray(vArr) Then Exit Function
    iDim = Me.DimCounter(vArr)

    If iDim = 1 Then
        If lDim <> 1 Then Exit Function
        lLast = lLbound + UBound(vArr) - LBound(vArr)
        ReDim vArr_Out(lLbound To lLast)
        For lRow = lLbound To lLast
            vArr_Out(lRow) = vArr(LBound(vArr) + lRow - lLbound)
        Next lRow
    ElseIf iDim = 2 Then
        Select Case lDim
            Case 1
                lLast = lLbound + UBound(vArr, 1) - LBound(vArr, 1)
                ReDim vArr_Out(lLbound To lLast, LBound(vArr, 2) To UBound(vArr, 2))
            Case 2
                lLast = lLbound + UBound(vArr, 2) - LBound(vArr, 2)
                ReDim vArr_Out(LBound(vArr, 1) To UBound(vArr, 1), lLbound To lLast)
            Case Else
                Exit Function
        End Select

        For lRow = LBound(vArr_Out, 1) To UBound(vArr_Out, 1)
            lRowSource = LBound(vArr, 1) + lRow - LBound(vArr_Out, 1)
            For lCol = LBound(vArr_Out, 2) To UBound(vArr_Out, 2)
                lColSource = LBound(vArr, 2) + lCol - LBound(vArr_Out, 2)
                vArr_Out(lRow, lCol) = vArr(lRowSource, lColSource)
            Next lCol
        Next lRow
    Else
        Exit Function
    End If

    Change_Lbound = vArr_Out
End Function