位置:首頁 > 高級語言 > VBA教學 > VBA LBound函數

VBA LBound函數

LBound 函數

LBound函數返回指定數組中最小的下標。因此,陣列的LBound為零。

語法:

LBound(ArrayName[,dimension])
  1. ArrayName, 必需的參數。該參數對應於所述數組的名稱。

  2. dimenstion, 一個可選的參數。這需要對應於所述陣列的維數的整數值。如果它是“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 : " & LBound(arr))

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

當執行函數輸出如下所示:

The smallest Subscript value of the given array is : 0
The smallest Subscript of the first dimension of arr2 is : 0
The smallest Subscript of the Second dimension of arr2 is : 0