API reference · Arrays
Change_Lbound
Rebase one dimension of a 1D or 2D array without changing its values or shape.
Signature#
Function Change_Lbound(ByVal vArr As Variant, ByVal lLbound As Long, ByVal lDim As Long) As VariantParameters#
| Parameter | VBA type | Default | Description |
|---|---|---|---|
vArr | VariantByVal | Required | Allocated 1D or 2D array to copy with different index bounds. |
lLbound | LongByVal | Required | New lower bound for the selected dimension. The new upper bound is this value plus the original dimension length minus one. |
lDim | LongByVal | Required | Dimension 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#
Before
amounts1D · (3,)
Indices 0..2 · one dimension
| 0 | 1 | 2 |
|---|---|---|
| 120 | 80 | 50 |
After
oneBased1D · (3,)
Indices 1..3 · one dimension
| 1 | 2 | 3 |
|---|---|---|
| 120 | 80 | 50 |
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.
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 SubBehavior and edge cases#
- The function reallocates and copies; it does not change bounds in place. Values keep their positional order.
- For a 2D matrix, changing row bounds leaves column bounds unchanged, and changing column bounds leaves row bounds unchanged.
- To make both dimensions one-based, call Change_Lbound once per dimension and assign each returned array.
- Bounds arithmetic uses Long. Requests whose calculated upper bound overflows Long, or whose allocation is not possible, can raise runtime errors.
- The input array is not modified. Empty, Null and error-valued elements are preserved as values.
Performance#
O(n) time and output storage for a 1D array, or O(r × c) for a 2D array.
View implementation
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