Excel VBA UBound Function
Excel VBA UBound Function: The UBound function in VBA returns the highest subscript for the specified dimension in the given array.
Syntax
UBound (ArrayName, [Dimension])
Parameter
ArrayName (required) – This parameter represents an array for which you want to find the highest subscript.
Dimension (optional) – This parameter specifies the dimension of the array, for which you require the highest subscript. By default, this parameter is set to 1.
Return
This function returns the highest subscript for the specified dimension in the given array.
Example 1
Sub UBoundFunction_Example() ' Returning the highest subscript for a 1-D array. Dim Array_Val(0 To 9) As Integer Dim UB_val As Integer UB_val = UBound(Array_Val) ' The integer UB_val will return a value equal to 10. Cells(1, 1).Value = UB_val End Sub
Output
9
Example 2
Sub UBoundFunction_Example2() ' Returning the highest subscript for a 2-D array. Dim Array_val(0 To 10, 0 To 20) As Double Dim UB_val1 As Double Dim UB_val2 As Double UB_val1 = UBound(Array_val, 1) UB_val2 = UBound(Array_val, 2) ' The integer UB_val1 will return a value equal to 10. Cells(1, 1).Value = UB_val1 ' The integer UB_val2 will return a value equal to 10. Cells(2, 1).Value = UB_val2 End Sub
Output
10
20