This documentation is archived and is not being maintained.

UBound Function

Returns the highest available subscript for the indicated dimension of an array.

Public Function UBound( _
   ByVal Array As System.Array, _ 
   Optional ByVal Rank As Integer = 1 _
) As Integer


Required. Array of any data type. The array in which you want to find the highest possible subscript of a dimension.
Optional. Integer. The dimension for which the highest possible subscript is to be returned. Use 1 for the first dimension, 2 for the second, and so on. If Rank is omitted, 1 is assumed.


Exception type Error number Condition
ArgumentNullException 9 Array is Nothing
RankException 9 Rank is less than 1 or Rank is greater than the rank of array.


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:

Dim A(100, 5, 4) As Byte
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.


This example uses the UBound function to determine the highest available subscript for the indicated dimension of an array.

Dim Highest, MyArray(10, 15, 20), AnyArray(6) as Integer
Highest = UBound(MyArray, 1)   ' Returns 10.
Highest = UBound(MyArray, 3)   ' Returns 20.
Highest = UBound(AnyArray)   ' Returns 6.


Namespace: Microsoft.VisualBasic

Module: Information

Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)

See Also

LBound Function | Dim Statement | ReDim Statement | ArgumentException Class | RankException Class