Upper Lower Bounds

The lowest and highest index numbers are known as bounds.
Arrays have both upper and lower bounds, and the elements of the array are contiguous within those bounds.
Array sizes should be identified using the LBound and UBound functions.
There are two functions that can be used to quickly obtain the upper and lower limits of an array:
LBOUND - Returns the smallest subscript in a given dimension of an array (Long).
UBOUND - Returns the largest subscript in a given dimension of an array (Long).
You can use these functions to determine the size of an existing array.


Dim arTesting(20) As String 
LBound(arTesting) = 0
LBound(arTesting,1) = 0

UBound(arTesting) = 20
UBound(arTesting,1) = 20

If the default lower bound is 0 then UBound is one less that the actual number of elements in the array.

Dim arTesting(5) As String 
Call MsgBox(UBound(arTesting)) = 5
Dim iArraySize As Integer
iArraySize = UBound(arTesting) + 1

If the lower bound is 1 then the number of elements is the value returned from UBound

Dim arTesting(1 to 5) As String 
Call MsgBox(UBound(arTesting)) = 5
Dim iArraySize As Integer
iArraySize = UBound(arTesting)

To be absolutely sure you can determine the size of an array it is best to always use the following formula:

Dim iArraySize As Integer 
iArraySize = UBound(arTesting) - LBound(arTesting) + 1

Dynamic Arrays

The LBound is very useful when working with dynamic arrays
Be aware that you must have initialised the dynamic array before calling the UBound or LBound functions.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext