UBound 函數

套用到
Microsoft 365 Access Access 2024 Access 2021 Access 2019 Access 2016

傳回 Long,其中包含陣列指定維度的最大可用下標。

語法

UBound (arrayname [, dimension ] )

UBound 函數語法具有下列引數:

引數 描述
arrayName 必要。 陣列變數的名稱;遵循標準變數命名慣例。
維度 可省略。 變 () 。 整數表示傳回哪個維度的上限。 第一個維度使用 1,第二個維度使用 2,依此類推。 如果省略 度,則會假設維度為 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

    

範例

注意

下列範例示範如何在 Visual Basic for Applications (VBA) 模組中使用此函數。 如需使用 VBA 的詳細資訊,請在 [搜尋] 旁的下拉式清單中選取 [開發人員參考],並在 [搜尋] 方塊中輸入一個或多個字詞。

此範例使用 UBound 函數來判斷陣列指定維度的最大可用下標。

Dim Upper
' Declare array variables.
Dim MyArray(1 To 10, 5 To 15, 10 To 20)
Dim AnyArray(10)
Upper = UBound(MyArray, 1)    ' Returns 10.
Upper = UBound(MyArray, 3)    ' Returns 20.
Upper = UBound(AnyArray)    ' Returns 10.