VBA LBound

Excel VBA LBound Function

LBound in VBA stands for “Lower Bound” i.e. it will extract the lowest number of an array. For example, if the array says “Dim ArrayCount (2 to 10) as String” then using LBound function we can find the least number of the array length i.e. 2.

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

Lbound (Array name [, dimension])
  • Array Name: This is the first argument. For this parameter, we need to specify the name of the array, which is used to define the array.
  • [Dimension]: If the array is a single dimension, this is not required. By default, it takes one, or else we need to supply the dimension number.

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

VBA-LBound

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

How to use the VBA LBound Function? (Example)

You can download this VBA LBound Excel Template here – VBA LBound Excel Template

Example #1

For example, look at the below code.

Code:

Sub LBound_Example1()

   Dim Count(2 To 5) As Integer

   MsgBox LBound(Count)

End Sub
VBA LBound Example 1

In the above code, we have defined the array as integer and the size of the array 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:

 Example 1.1

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

Example #2

Now, look at the below code.

Code:

Sub LBound_Example2()

  Dim Count(5) As Integer

  MsgBox LBound(Count)

End Sub
VBA LBound Example 2

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

Output:

 Example 2.1

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 data range and decide the lower bound from the range of data. For example, look at the below data image.

VBA LBound Example 3

From this range, we will decide the smallest and highest row size.

First, define the variable as a variant.

Code:

Sub LBound_Example3()

  Dim Rng As Variant

End Sub
VBA LBound Example 3.1.0

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

Code:

Sub LBound_Example3()

  Dim Rng As Variant

  Rng = Range("B2:B5").Value

End Sub
VBA LBound Example 3.2

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

Code:

Sub LBound_Example3()

  Dim Rng As Variant

  Rng = Range("B2:B5").Value

  MsgBox LBound(Rng)

End Sub
Example 3.3

Now run the VBA code and see the lowest value from the length.

Output:

VBA LBound Example 3.4

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

For this range, we will find the lower bound and upper bound values.

Code:

Sub LBound_Example3()

  Dim Rng As Variant

  Rng = Range("A2:B5").Value

End Sub
 Example 3.5

Since we have more than one dimension array, we need to supply the dimension number as well.

Code:

Sub LBound_Example3()

  Dim Rng As Variant

  Rng = Range("A2:B5").Value

  MsgBox LBound(Rng, 1)

End Sub
Example 3.6

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

Code:

Sub LBound_Example3()

  Dim Rng As Variant

  Rng = Range("A2:B5").Value

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

End Sub
VBA LBound Example 3.7

This will find the first column lower length and upper length. Similarly, in the next line, write one more message box but this time, change the dimension from 1 to 2.

Code:

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
Example 3.8

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

Output:

VBA LBound Example 3.9

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

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

Output:

 Example 3.10

For the second dimension lower limit is 1, and the upper limit is 2.

Things to Remember here

  • LBound function returns the minimum length from the array.
  • When the array length static, i.e., a single number, then 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 has been a guide to VBA LBound. Here we discuss how to use 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 –

  • 35+ Courses
  • 120+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>