Vba-ubound-function

提供:Dev Guides
移動先:案内検索

VBA-UBound関数

UBound関数は、指定された配列の最大の添え字を返します。 したがって、この値は配列のサイズに対応します。

構文

UBound(ArrayName[,dimension])

パラメータ説明

  • ArrayName -必須パラメーター。 このパラメーターは、配列の名前に対応しています。
  • Dimension -オプションのパラメーター。 これは、配列の次元に対応する整数値を取ります。 '1’の場合、最初の次元の下限を返します。 '2’の場合、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 smallest Subscript value of the given array is : 5
The smallest Subscript of the first dimension of arr2 is : 3
The smallest Subscript of the Second dimension of arr2 is : 2