VBA UBound()函數

LBound()函數返回指定數組的最大下標。 因此,這個值對應於數組的大小。

語法

UBound(ArrayName[,dimension])

參數說明

  • ArrayName - 必需的參數。該參數對應於數組的名稱。
  • Dimension - 一個可選參數。 這需要一個與數組的維度相對應的整數值。如果是“1”,則返回第一維的下界; 如果是“2”,則返回第二維的下界,依此類推。

例子

添加一個模組,並添加以下代碼 -

Private Sub Constant_demo_Click()
   Dim arr(5) as Variant
   arr(0) = "1"           'Number as String
   arr(1) = "VBScript     'String
   arr(2) = 100           'Number
   arr(3) = 2.45          'Decimal Number
   arr(4) = #10/07/2013#  'Date
   arr(5) = #12.45 PM#    'Time
   msgbox("The smallest Subscript value of  the given array is : " & UBound(arr))

   ' For MultiDimension Arrays :
   Dim arr2(3,2) as Variant
   msgbox("The smallest Subscript of the first dimension of arr2 is : " & UBound(arr2,1))
   msgbox("The smallest Subscript of the Second dimension of arr2 is : " & UBound(arr2,2))
End Sub

當執行上面的函數時,它會產生下麵的輸出。

The Largest Subscript value of the given array is : 5
The Largest Subscript of the first dimension of arr2 is : 3
The Largest Subscript of the Second dimension of arr2 is : 2

上一篇: VBA數組 下一篇: VBA用戶自定義函數