×

VBA UBound

VBA UBound 

The UBound or Upper Bound function in VBA is used to specify the length of an array and returns the highest subscript for a dimension for the specified array. It is the vice versa for LBOUND or Lower Bound function. For example, if you specify an array object with upper bound as 20, MyArray(20). This array object can hold 21 values as the array calculation starts from 0. So, 20 means +1 i.e., total of 21 values.

Syntax

UBound(arrayname, [ dimension ])

Parameters

Arrayname (required)- This parameter represents the array for which you want to find the highest subscript.

Dimension (optional) – This parameter specifies the dimension for the array (you need the highest subscript). It could be one dimensional, two dimensional or multi-dimensional. The integer value 1 is used to represent the first dimension, and 2 is used for signifying the second dimension, and so on. The default value is 1.

Return

The UBOUND function returns the highest subscript for a dimension for the specified array.

Example 1

Write a macro to demonstrate the example of UBOUND Function.

Code:

Sub UBOUND_Example1()
 'defining the array with UBOUND as 5
     Dim arry(5) As Variant
     arry(0) = 31               'Integer Value
     arry(1) = "Hello VBA"      'String Value
     arry(2) = "@#$%"           'symbol as string 
     arry(3) = 12.415            'Decimal Number
     arry(4) = #10/7/2013#      'Date
     'Displaying the output of Upper Bound function
     MsgBox ("The UBOUND function value for the specified array: " & UBound(arry))
 End Sub 

Let’s work with the set-by-step code of lines:

Step 1: Open the VBA developer tab either by using the shortcut keywords Alt +F11 or click on developer window -> visual basic editor.

Step 2: Visual Basic Editor will open. The next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA UBound

Step 3: In the VBA Module window, within the sub-block, introduce your macro name.

VBA UBound

Step 4: Define the array object and we will only specify the UBOUND value. Unlike here, we have defined the array object as Arry(5), where 5 represents the upper bound value.

VBA UBound

Step 5: Specify the array length one by one with respective values.

Note: The array size starts with zero. So, in total, it can store 6 values.  

VBA UBound

Step 6: At last, we will display the upper bound’s value with the help of MsgBox.

VBA UBound

Output

Step 7: Execute the above code either by pressing the F5 shortcut key or by clicking on the Run button.

Step 8: You will notice that the message dialog box has pop up displaying the upper bound value.

VBA UBound

Example 2

Write a Code to demonstrate the example of Upper to copy the data of one worksheet automatically to another.

Code:

Sub Ubound_Example2()
     'declaring an array variable
     Dim Rnge() As Variant
     'activating the Sheet2
     Sheets("Sheet2").Activate
     'selecting the active data in the sheet 
     Rnge = Range("A1", Range("A1").End(xlDown).End(xlToRight))
     Worksheets.Add
     'With the help of UBound Function we will offset the cells by the maximum length
     'and the retuned range value will be equal to the "Rnge" array value
     Range(ActiveCell, ActiveCell.Offset(UBound(Rnge, 1) - 1, UBound(Rnge, 2) - 1)) = Rnge
 End Sub 

Let’s work with the set-by-step code of lines:

Step 1: Open the VBA developer tab either by using the shortcut keywords Alt +F11 or click on developer window -> visual basic editor.

Step 2: Visual Basic Editor will open. The next step is to create a module. Right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA UBound

Step 3: In the VBA Module window, within the sub-block, introduce your macro name

VBA UBound

Step 4: Define an array variable.

VBA UBound

Step 5: Next, we will update the sheet where our data is kept. Here we have mentioned ‘Sheet2’.

VBA UBound

Step 6: Allocate the variable range of cells containing your data. We have specified the first cell as A2 and, with the help of xlDown and xlToRight, have selected all the rightmost and bottom filled cells.

VBA UBound

Step 7: We will add a new worksheet so as we can paste the data over here.

VBA UBound

Step 8: With the help of UBound Function, we will offset the cells by the maximum length and the retuned range of cells we will paste the preciously stored array variable’s (“Rnge”) data.

VBA UBound

Output

Step 9: Execute the above code either by pressing the F5 shortcut key or by clicking on the Run button.

Step 10: You will notice that another sheet has been added and your data has been successfully pasted on that sheet.

VBA UBound

Related Topics

Excel VBA Choose Function

VBA Choose Function: The Choose function in VBA chooses function selects the corresponding value from a list of arguments depending as per the specified index. Syntax Choose (Index, [Choice-1], [Choice-2], ...) Parameter Index (required) – This...

2 minutes read.

Excel VBA DateSerial Function

The DateSerial function in VBA returns a Date from a supplied year, month, and day number. Syntax DateSerial (Year, Month, Day) Parameter Year (required) – This parameter represents an integer signifying the year. Month (required) – This parameter...

2 minutes read.

VBA Find Function

VBA Find Function The Excel VBA FIND function finds any information in your Excel. It can be used on a Range object on the worksheet. It works the same, unlike the Excel Find &...

6 minutes read.

For Next loop in VBA

For Next Loop The ”For Next” loop is used for a fixed number of times. It works by implementing the loop for the specified number of times. In this, the user specifies how...

3 minutes read.

Excel VBA CDbl Function

VBA CDbl Function: The CDbl function in VBA converts an expression into a Double data type. Syntax CDbl (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.

Excel VBA CDate Function

VBA CDate Function: The CDate function in VBA converts an expression into a Date (or Time) data type. Syntax CDate (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to...

1 minute read.

Excel VBA Conditional Statement

Conditional Statement in VBA Excel Conditional Statements in Excel VBA are one of the most powerful and useful features in programming, this will give you to perform comparisons to decide or...

2 minutes read.

Excel VBA CCur Function

The CCur function in VBA is used to convert an expression into a Currency data type. It can take a maximum of 15 digits to the left of the decimal place and...

1 minute read.

Excel VBA Mid Function

VBA Mid Function: The Mid function in VBA returns a substring from within a supplied string. Syntax Mid (Str, Start, [Length]) Parameter Str (required) -This parameter represents a string from which you want to extract the substring. Start...

1 minute read.

Excel VBA CDec Function

VBA CDec Function: The CDec function in VBA converts an expression into a Decimal data type. Syntax CDec (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to a...

1 minute read.

Excel VBA Time Function

Excel VBA Time Function: The Time function in VBA returns the current time. Syntax Time () Parameter NA Return This function returns the current time.  Example 1 Sub TimeFunction_Example1() ' returns the current time Dim time_val...

1 minute read.

Debugging in Excel VBA

Debugging in VBA: Debugging is a technique used to fix errors in programming languages. In Excel VBA, we have different ways by which you can identify the error in the...

2 minutes read.

Excel VBA MessageBox

Message Box The MsgBox in Excel VBA is a dialog box used to inform the users of your program by showing a custom message or get some necessary inputs such as Yes/No or...

4 minutes read.

Excel VBA AutoFilter

One of the reasons for Excel VBA’s popularity is its capability to filter and analyze data from huge database with the help of a method known as AutoFilter. This method permits a...

4 minutes read.

Excel VBA Left Function

VBA Left Function: The Left function in VBA returns a substring from the start of the specified string. Syntax Left (Str, Length) Parameter Str (required) – This parameter represents the string that you want to extract...

1 minute read.

Excel VBA: DO WHILE….Loop

DO WHILE….Loop The “Do While” Loop is the same, unlike the FOR statement, just that it will keep on looping till the specified condition is true. It is used when we want to...

3 minutes read.

Excel VBA Array Function

VBA Array Function: The Array function in VBA generates an array containing the given set of values. Syntax Array (Arglist) Parameter Arglist (required) – This parameter the list of values that you want to make...

2 minutes read.

VBA UBound

VBA UBound  The UBound or Upper Bound function in VBA is used to specify the length of an array and returns the highest subscript for a dimension for the specified array. It is...

3 minutes read.

Excel VBA Rnd Function

VBA Rnd Function: The Rnd function in VBA returns a random number that is greater than or equal to (>=) 0 and is less than (<) 1. Syntax Rnd ([Number]) Parameter Number (optional) –This...

1 minute read.

VBA Color Index Property

What is Color Index Property? The Excel VBA Color Index is used to change the color for the cell or range of cells or text (located under the Font section). It sets the color...

5 minutes read.