VBA LBound配列関数の使用方法は?(例付き)

Excel VBALBound関数

VBAのLBoundはLowerBound」の略で、配列の最小数を抽出します。たとえば、配列に「Dim ArrayCount(2 to 10)as String」と表示されている場合、LBound関数を使用すると、配列の長さの最小数、つまり2を見つけることができます。

以下は、LBound関数の構文です。それは非常にシンプルで簡単で、2つのパラメーターしかありません。

Lbound(配列名[、次元])
  • 配列名:これは最初の引数です。このパラメーターには、配列の定義に使用される配列の名前を指定する必要があります。
  • [次元]:配列が単一次元の場合、これは必須ではありません。デフォルトでは1次元であるか、次元番号を指定する必要があります。

したがって、これらの関数を使用して、配列の最小の長さを見つけることができます。

VBA LBound関数の使用方法は?(例)

このVBALBound Excelテンプレートはここからダウンロードできます– VBA LBoundExcelテンプレート

例1

例については、以下のコードを見てください。

コード:

 Sub LBound_Example1()Dim Count(2 To 5)As Integer MsgBox LBound(Count)End Sub 

上記のコードでは、配列を整数として定義し、配列のサイズを2〜5として定義しました。次に、LBound関数を使用して、配列の最小の長さを表示するVBAメッセージボックスを割り当てました。

コードを実行すると、メッセージボックスに次の結果が表示されます。

出力:

配列は2から始まるため、LBound関数は配列の最小の長さを2として決定します。

例2

次に、以下のコードを見てください。

コード:

 Sub LBound_Example2()Dim Count(5)As Integer MsgBox LBound(Count)End Sub 

上記では、下限を決定していませんが、配列の長さを5として指定しただけです。次に、コードを実行して、値の最小の長さを確認しましょう。

出力:

配列の開始と終了を決定しない場合は、静的な数値を指定するため、結果は0として返されます。たとえば、「Count(5)、つまりこの場合、配列の値は1からではなく0から始まります。現在、合計6つの値を格納できます。

カウント(0)、カウント(1)、カウント(2)、カウント(3)、カウント(4)、カウント(5)。

例3

次に、データ範囲を使用して、データ範囲から下限を決定します。例については、以下のデータ画像をご覧ください。

この範囲から、最小行サイズと最大行サイズを決定します。

まず、変数をバリアントとして定義します。

コード:

 Sub LBound_Example3()Dim Rng As Variant End Sub 

この「Rng」バリアント変数の場合、範囲参照値を「Range( "B2:B5")。Value」として設定します。

コード:

 Sub LBound_Example3()Dim Rng As Variant Rng = Range( "B2:B5")。Value End Sub 

この範囲では、配列の最小長と最大長がわかります。メッセージボックスとLBound関数を開き、変数名を指定します。

コード:

 Sub LBound_Example3()Dim Rng As Variant Rng = Range( "B2:B5")。Value MsgBox LBound(Rng)End Sub 

次に、VBAコードを実行して、長さから最小値を確認します。

出力:

次に、変数参照をB2:B5からA2:B5に変更します。

この範囲では、下限値と上限値が見つかります。

コード:

 Sub LBound_Example3()Dim Rng As Variant Rng = Range( "A2:B5")。Value End Sub 

複数の次元配列があるため、次元番号も指定する必要があります。

コード:

 Sub LBound_Example3() Dim Rng As Variant Rng = Range("A2:B5").Value MsgBox LBound(Rng, 1) End Sub 

To find the first column first lower bound above code will help, similarly to find the upper bound in this first column below code will help.

Code:

 Sub LBound_Example3() Dim Rng As Variant Rng = Range("A2:B5").Value MsgBox LBound(Rng, 1) & vbNewLine & UBound(Rng, 1) End Sub 

This will find the first column lower length and upper length. Similarly in the next line write one more message box but this time change the dimension from 1 to 2.

Code:

 Sub LBound_Example3() Dim Rng As Variant Rng = Range("A2:B5").Value MsgBox LBound(Rng, 1) & vbNewLine & UBound(Rng, 1) MsgBox LBound(Rng, 2) & vbNewLine & UBound(Rng, 2) End Sub 

Run the code and see the result in the message box.

Output:

For the first dimension lower bound is 1 and the upper bound is 4.

Click on “Ok” to get the next dimension limits.

Output:

For the second dimension lower limit is 1 and the upper limit is 2.

Things to Remember here

  • LBound function returns the minimum length from the array.
  • When the array length static i.e. single number then array always starts from the number 0 not from 1.
  • In the case of a multi-dimensional array, we need to specify the dimension number.