×

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

UserForm and its Properties Excel VBA

UserForm and its Properties Userform has certain properties that can be viewed as category wise (based on appearance, behavior, font) or in an alphabetic manner. The property window is used to set or...

11 minutes read.

Excel VBA GoTo Statement

GoTo Statement he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends...

2 minutes read.

VBA ActiveCell Property

What is the ActiveCell Property? The active cell signifies the active selected cell in the current worksheet. The Active property acts as a reference point and is used to move the cell cursor...

5 minutes read.

Excel VBA User-Defined Functions

User-Defined Functions One of the advantages of VBA is that you can create your own functions using macros. These functions can be called and used as other functions in excel and use them. You can...

3 minutes read.

VBA VBScript Regex Methods Regex

VBA VBScript Regex Methods Regex The VBA Regex supports 3 methods which are as follows: ExecuteReplaceText Execute The execute method is used to extract a match from the given based on the defined matching...

3 minutes read.

If ElseIf ElseIf Statement or Nested If statement in VBA

VBA Excel: If … ElseIf … ElseIf Statement or Nested If statement This function enables you to check multiple conditions and, based on that, then run one of the statement blocks present. If...

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 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.

Excel VBA Sqr Function

VBA Sqr Function: The Sqr function in VBA returns the square root for the specified number. Syntax Sqr (Number) Parameter Number (required) – This parameter represents a positive numeric value that you want to calculate...

1 minute 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 Error Handling

What is Errors and Types of Error? Errors are conditions that resist the flow of the program or enables a problem while running any programming. There are three types of errors in VBA...

5 minutes read.

Excel VBA Filter Function

VBA Filter Function: The Filter function in VBA returns a subset for the given string array, based on specified criteria. Syntax Filter (SourceArray, Match, [Include], [Compare]) Parameter SourceArray (required) – This parameter the array of Strings that you...

2 minutes read.

Excel VBA Chr Function

VBA Chr Function: The Chr function in VBA returns the character equivalent to a supplied character code between 0 and 255. Syntax Chr (CharCode) Parameter CharCode (required) – This parameter represents the character code for...

1 minute read.

VBA Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 minutes read.

Steps to Create a Chart in VBA

Steps to Create a Chart Charts are created either by directly working with the chart variable object that defines the chart data or by ChartObject method. In order to get to...

3 minutes read.

Excel VBA For Each Loop

A For Each loop executes a statement or a group of statements for each element in an array or collection. It repeats the statement/condition/code for each element in a collection. For Each Loops loop through every...

4 minutes read.

Excel VBA: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

Excel VBA Sgn Function

VBA Sgn Function: The Sgn function in VBA returns an integer (+1, 0, or -1), stating the arithmetic sign for the specified number. Syntax Sgn (Number) Parameter Number (required) –This parameter represents the number that...

1 minute read.

VBA ListBox

What is a ListBox? ListBox refers to a permanently displayed control (usually box-shaped) which contains a list of objects (or attribute, or elements) from which the user can select single or multiple attributes....

8 minutes read.

Excel Objects in VBA

What are Excel Objects? The Excel objects belong to the entities that make up an Excel Workbook, Worksheets, Columns, Rows, Cell Ranges, etc. Each object in Excel has loads of Properties that are...

6 minutes read.