VBA – UBound Function

  • Post author:
  • Post category:VBA
  • Post comments:0 Comments
UBound Function

The UBound Function returns the largest subscript of the specified array. Hence, this value corresponds to the size of the array.

Syntax

UBound(ArrayName[,dimension])

Parameter Description

  • ArrayName − A required parameter. This parameter corresponds to the name of the array.
  • Dimension − An optional parameter. This takes an integer value that corresponds to the dimension of the array. If it is ‘1’, then it returns the lower bound of the first dimension; if it is ‘2’, then it returns the lower bound of the second dimension, and so on.

Example

Add a button and add the following function.

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

When you execute the above function, it produces the following output.

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

Previous Page:-Click Here

Leave a Reply