VBA - Fonction LBound

La fonction LBound renvoie le plus petit indice du tableau spécifié. Par conséquent, LBound d'un tableau est ZERO.

Syntaxe

LBound(ArrayName[,dimension])

Description des paramètres

  • ArrayName- Un paramètre obligatoire. Ce paramètre correspond au nom du tableau.

  • Dimension- Un paramètre facultatif. Cela prend une valeur entière qui correspond à la dimension du tableau. S'il vaut «1», alors il renvoie la limite inférieure de la première dimension; s'il vaut '2', alors il renvoie la limite inférieure de la deuxième dimension et ainsi de suite.

Exemple

Ajoutez un bouton et ajoutez la fonction suivante.

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

Lorsque vous exécutez la fonction ci-dessus, elle produit la sortie suivante.

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