VBA - Função LBound

A função LBound retorna o menor subscrito da matriz especificada. Portanto, LBound de uma matriz é ZERO.

Sintaxe

LBound(ArrayName[,dimension])

Descrição do Parâmetro

  • ArrayName- Um parâmetro obrigatório. Este parâmetro corresponde ao nome da matriz.

  • Dimension- Um parâmetro opcional. Isso leva um valor inteiro que corresponde à dimensão da matriz. Se for '1', então ele retorna o limite inferior da primeira dimensão; se for '2', retorna o limite inferior da segunda dimensão e assim por diante.

Exemplo

Adicione um botão e adicione a seguinte função.

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

Quando você executa a função acima, ela produz a seguinte saída.

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