TOP

VBA function: UBound

Description

The VBA UBound function returns the largest available index for the specified array.


UBound Syntax

UBound(array)

Or

UBound(array, dimension)

UBound VBA Example

Using the UBound function to get the index of each of the 2 dimensions of the array:

Sub UBoundExample1()

      Dim array(10, 4)
    
      'The maximum index of the first dimension
      MsgBox UBound(array) 'Returns: 10
    
      'The maximum index of the second dimension
      MsgBox UBound(array, 2) 'Returns: 4

End Sub

Using the UBound function to get the number of values in an array created by the SPLIT:

Sub UBoundExample2()

      link = "www.moonexcel.com.ua"
    
      'Split a string of characters into an array
      array = Split(link, ".")
    
      'The number of array elements (knowing that the array starts at 0)
      number = UBound(array) + 1
    
      'Display the number of array elements
      MsgBox number 'Returns: 4

End Sub