UBound Function

See Also33B8A6P                 Example2MJ50XV>Low

Returns the largest available subscript for the indicated dimension of an arrayYPCGZO.

Syntax

UBound(array [, dimension])

Remarks

The UBound function is used with the LBound function to determine the size of an array.  Use the LBound function to find the lower limit of an array dimension.

The UBound function has these parts:

Part                 Description

 

array                Name of an array variable.

dimension         Integer indicating which dimension's upper bound is returned.  Use 1 for the first dimension, 2 for the second dimension, and so on.  If dimension is omitted, 1 is assumed.

 

UBound returns the values listed in the table below for an array with these dimensions:

  Dim A(1 To 100, 0 To 3, -3 To 4)

 

Statement        Return Value

 

UBound(A, 1)    100

UBound(A, 2)    3

UBound(A, 3)    4

 


See Also

Dim StatementLANDIM

Global StatementTDN897

LBound FunctionY0NKEZ

Option Base StatementQH5EUA

ReDim StatementQ1CGU1

Static StatementCC4JTI


UBound Function Example

The example uses the UBound function to determine the upper bounds for a three-dimensional array.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

Sub Form_Click ()

  Dim A, B, C, Msg, NL, TB                  ' Declare variables.

  Dim Array()                               ' Declare array variable.

  NL = Chr(10): TB = Chr(9)                 ' Define newline, tab.

  ' Generate some random dimensions between 2 and 10 for array size.

  A = -Int(9 * Rnd + 2)                     ' First dimension.

  B = Int(9 * Rnd + 2)                      ' Second dimension.

  C = Int(9 * Rnd + 2)                      ' Third dimension.

  ReDim Array(A To 20, B to 20 , C to 20)   ' Set dimensions.

  Msg = "The test array has the following lower bounds: " & NL

  Msg = Msg & TB & "Dimension 1 = " & UBound(Array, 1) & NL

  Msg = Msg & TB & "Dimension 2 = " & UBound(Array, 2) & NL

  Msg = Msg & TB & "Dimension 3 = " & UBound(Array, 3)

  MsgBox Msg                                ' Display message.

End Sub