Name

UBound Function

Syntax

UBound(arrayname[, dimension])
arrayname

Use: Required

The name of the array.

dimension

Use: Optional

Data Subtype: Long

A number specifying the dimension of the array.

Return Value

A Variant of subtype Long.

Description

Indicates the upper limit of a specified dimension of an array. The upper boundary is the largest subscript you can access within the specified array.

Rules at a Glance

  • If dimension isn’t specified, 1 is assumed. To determine the upper limit of the first dimension of an array, set dimension to 1, to 2 for the second dimension, and so on.

  • The upper bound of an array dimension can be set to any integer value using Dim, Private, Public, and Redim.

Programming Tips & Gotchas

  • Note that UBound returns the actual subscript of the upper bound of a particular array dimension.

  • UBound is especially useful for determining the current upper boundary of a dynamic array.

  • The UBound function works only with conventional arrays. To determine the upper bound of a collection object, retrieve the value of its Count or length property.

VBA/VBScript Differences

In VBA, the UBound function is frequently used along with the LBound function to enumerate the elements of an array, as in the following code fragment:

For intCtr = LBound(aArray) To UBound(aArray)

However, although VBScript supports the LBound function, it does not allow you to define the lower bound of any array. As a result, there is never a reason to call the VBScript LBound function.

See Also

LBound Function

Get VBScript in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.