VBA LBound()函数

LBound()函数返回指定数组的最小下标。 因此,数组的LBound的结果是零。

语法

LBound(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 : " & 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

上一篇: VBA数组 下一篇: VBA用户自定义函数