Since array subscripts start at 0, the length of a dimension is greater by one than the highest available subscript for that dimension.
For an array with the following dimensions, UBound returns the values in the following table:
Call to UBound
|
Return value
|
|---|
UBound(a, 1)
|
100
|
UBound(a, 2)
|
5
|
UBound(a, 3)
|
4
|
You can use UBound to determine the total number of elements in an array, but you must adjust the value it returns to account for the fact that the subscripts start at 0. The following example calculates the total size of the array a in the preceding example:
Dim total As Integer
total = (UBound(A, 1) + 1) * (UBound(A, 2) + 1) * (UBound(A, 3) + 1)
The value calculated for total is 3030, which is 101 * 6 * 5.