Excel VBA LBound Function

Below is the syntax of the LBound function. It is very simple and easy. It has only two parameters.

Lbound (Array name [, dimension])

So, using these functions, we can find the minimum length of an array.

You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA LBound (wallstreetmojo.com)

How to use the VBA LBound Function? (Example)

Example #1

Look at the below code.

Code:

Sub LBound_Example1()

Dim Count(2 To 5) As Integer

MsgBox LBound(Count)

End Sub

In the above code, we have defined the array as integer and the array size as 2 to 5. Next, we have assigned the VBA message box to show the lowest length of the array by using the LBound function.

When we run the code, we will get the below result in a message box.

Output:

Since our array starts from 2, the LBound function determines the lowest length of the array as 2.

Example #2

Sub LBound_Example2()

Dim Count(5) As Integer

MsgBox LBound(Count)

End Sub

In the above, we have not decided on the lowest limit. Rather, we just supplied the array length as 5 now. So, let us run the code and see the lowest length of the value.

It has returned the result as 0 because when we don’t decide the start and endpoint of an array, rather provide the static number, for example, “Count (5), i.e., in this case, array value starting from 0, not from 1. So, we can store a total of 6 values in it now.

Count (0), Count(1), Count(2), Count(3), Count(4), Count(5).

Example #3

Now, we will use the data range and decide the lower bound from the range of data. For example, look at the below data image.

We will decide on the smallest and highest row size from this range.

First, define the variable as a variant.

Sub LBound_Example3()

Dim Rng As Variant

End Sub

For this “Rng” variant variable, set the range reference value as “Range (“B2: B5”).Value.”

Sub LBound_Example3()

Dim Rng As Variant

Rng = Range(“B2:B5”).Value

End Sub

For this range, we will find the lowest and highest array length. Next, open the message box and LBound function and supply the variable name.

Sub LBound_Example3()

Dim Rng As Variant

Rng = Range(“B2:B5”).Value

MsgBox LBound(Rng)

End Sub

Now, run the VBA codeVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more and see the lowest value from the length.

Now, change the variable reference from B2: B5 to A2: B5.

We will find the lower and upper bound values for this range.

Sub LBound_Example3()

Dim Rng As Variant

Rng = Range(“A2:B5”).Value

End Sub

Since we have more than one dimension array, we must also supply the dimension number.

Sub LBound_Example3()

Dim Rng As Variant

Rng = Range(“A2:B5”).Value

MsgBox LBound(Rng, 1)

End Sub

To find the first column first lower bound above code will help similarly. Likewise, finding the upper bound in this first column below the code will help.

Sub LBound_Example3()

Dim Rng As Variant

Rng = Range(“A2:B5”).Value

MsgBox LBound(Rng, 1) & vbNewLine & UBound(Rng, 1)

End Sub

It will find the first column’s lower length and upper length. Similarly, write one more message box in the next line but dimension from 1 to 2.

Sub LBound_Example3()

Dim Rng As Variant

Rng = Range(“A2:B5”).Value

MsgBox LBound(Rng, 1) & vbNewLine & UBound(Rng, 1)

MsgBox LBound(Rng, 2) & vbNewLine & UBound(Rng, 2)

End Sub

Run the code and see the result in the message box.

For the first dimension, the lower bound is 1, and the upper bound is 4.

Click on “OK” to get the next dimension limits.

The second dimension’s lower limit is 1, and the upper limit is 2.

Things to Remember here

  • The LBound function returns the minimum length from the array.When the array length is static, i.e., a single number, the array always starts from the number 0, not from 1.In the case of a multi-dimensional array, we need to specify the dimension number.

This article has been a guide to VBA LBound. Here, we discuss using the VBA LBound function to find the least number of the array length in Excel, along with practical examples and downloadable templates. Below are some useful articles related to VBA: –

  • VBA StrComp FunctionVBA Square Root FunctionSendKeys Method in VBAUBOUND in VBAVBA FreeFile