UBound 函数
返回一个 Long 型数据,其值为指定的数组维可用的最大下标。
语法
UBound(arrayname[, dimension])
UBound 函数的语法包含下面部分:
部分 | 描述 |
---|---|
arrayname | 必需的。数组变量的名称,遵循标准变量命名约定。 |
dimension | 可选的;Variant (Long)。指定返回哪一维的上界。1 表示第一维,2 表示第二维,如此等等。如果省略 dimension,就认为是 1。 |
说明
UBound 函数与 LBound 函数一起使用,用来确定一个数组的大小。LBound 用来确定数组某一维的上界。
对具有下述维数的数组而言,UBound 的返回值见下表:
Dim A(1 To 100, 0 To 3, -3 To 4)
语句 | 返回值 |
---|---|
UBound(A, 1) | 100 |
UBound(A, 2) | 3 |
UBound(A, 3) | 4 |
UBound 函数示例
该示例使用 UBound 函数,确定数组的指定维的最大可用下标。
Dim Upper
Dim MyArray(1 To 10, 5 To 15, 10 To 20) '声明数组变量。
Dim AnyArray(10)
Upper = UBound(MyArray, 1) '返回 10。
Upper = UBound(MyArray, 3) '返回 20。
Upper = UBound(AnyArray) '返回 10。
source:VBA Help