JIYIK CN >

Current Location:Home > Learning > PROGRAM > Vba >

Getting the length of an array in VBA

Author:JIYIK Last Updated:2025/04/16 Views:

Arrays are a popular tool in computer programming where you can save required information for later use or during code execution. Almost every computer programming language includes arrays in their libraries because sometimes the need to save and retrieve information is inevitable.

In VBA, getting the length of an array means counting the number of elements present in the array. To do this, you must know the lowest and highest element indexed. Then, the difference between the highest and the lowest will be the array length.

Additionally, you can use COUNTAworksheet functions to obtain the same information.

This article uses the highest/lowest index difference and COUNTAworksheet functions to get the length of a VBA array.

Taking the difference of highest and lowest index to get the length of an array in VBA

The logic of this method is that we can get the length of the array by subtracting the highest index from the lowest index and then adding 1. We add 1 because we need to include the lowest index in the length of the array.

The following code block will demonstrate how to get the array length of a fixed length array. Note that it is not always correct to just get the highest index to get the array length, because in some cases the lowest index is not equal to 0 and can start from a different index.

Sub ArrayLengthDemo()

Dim stringArr(5 To 9) As String

stringArr(5) = "Glen"
stringArr(6) = "Yumi"
stringArr(7) = "Myla"
stringArr(8) = "Katrina"
stringArr(9) = "Jose"

Debug.Print "The array length of stringArr is " UBound(stringArr) - LBound(stringArr) + 1
End Sub

ArrayLengthDemoOutput:

The array length of stringArr is  5

The following code block will demonstrate how to get the array length of a dynamic array.

Sub ArrayLengthDemo()

Dim StringArr As Variant

StringArr = Array("Glen", "Yumi", "Katrina", "Myla", "Jose")

Debug.Print "The array length of StringArr is "; UBound(StringArr) - LBound(StringArr) + 1

End Sub

ArrayLengthDemoOutput:

The array length of stringArr is  5

I will demonstrate implementing a function to get the length of an array in the following code. This will be helpful if you often need to get the length of an array.

Additionally, the code below has extra logic to detect if the array being measured is empty.

Public Function GetArrayLength(arr As Variant) As Integer
   If IsEmpty(arr) Then
      GetArrayLength = 0
   Else
      GetArrayLength = UBound(arr) - LBound(arr) + 1
   End If
End Function

Sub GetArrayLengthDemo1()


Dim stringArr(5 To 9) As String

stringArr(5) = "Glen"
stringArr(6) = "Yumi"
stringArr(7) = "Myla"
stringArr(8) = "Katrina"
stringArr(9) = "Jose"

Debug.Print "The array length is " & GetArrayLength(stringArr)

End Sub

Sub GetArrayLengthDemo2()

Dim NullArr As Variant

Debug.Print "The array length is " & GetArrayLength(NullArr)

End Sub

GetArrayLengthDemo1Output:

The array length is 5

GetArrayLengthDemo2Output:

The array length is 0

COUNTAUsing Worksheet Functions in VBA to Get Array Length

An array is a collection of elements structured in a single or multidimensional manner. You can use COUNTAworksheet functions to calculate these elements in a single line of code.

The following code block demonstrates how to use COUNTAthe worksheet function to get the length of an array.

grammar:

WorksheetFunction.CountA([ arrayName ])

scope:

   
[arrayName] The array to measure

COUNTAWorksheet functions are limited to 30 elements. If you are working with large arrays, the first method is recommended.

Sub ArrayLengthDemo()

Dim stringArr(5 To 9) As String

stringArr(5) = "Glen"
stringArr(6) = "Yumi"
stringArr(7) = "Myla"
stringArr(8) = "Katrina"
stringArr(9) = "Jose"

Debug.Print "The array length of stringArr is " & WorksheetFunction.CountA(stringArr)
End Sub

ArrayLengthDemoOutput:

The array length of stringArr is 5
Sub ArrayLengthDemo()

Dim StringArr As Variant

StringArr = Array("Glen", "Yumi", "Katrina", "Myla", "Jose")

Debug.Print "The array length of StringArr is " & WorksheetFunction.CountA(stringArr)

End Sub

ArrayLengthDemoOutput:

The array length of stringArr is  5 

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Remove duplicates in VBA

Publish Date:2025/04/16 Views:104 Category:Vba

We will explain how to remove duplicates in VBA by example. Remove duplicates in VBA When working with an Excel worksheet that contains a lot of data, it is likely that this data contains some duplicates. Any duplicates must be removed to a

Rounding in VBA

Publish Date:2025/04/16 Views:162 Category:Vba

Round() We will introduce the and functions in VBA through examples RoundUp() . Round() Using the or RoundUp() function in VBA When working with Excel worksheets containing numbers and calculations, we get results that are fractions. Someti

Adding a new row in VBA

Publish Date:2025/04/16 Views:194 Category:Vba

We will show you how to continue the code on the next line in VBA by example. We will also show you how to go to the next line in a message box using different methods in VBA. Adding a new row in VBA There are two different situations in pr

Sum function in VBA

Publish Date:2025/04/16 Views:126 Category:Vba

We will show you how to use sum in VBA. Sum function in VBA Sum is the most commonly used function in excel. This function is very useful as we can use sum to get the total from the financial table. We will learn how to use the sum function

Exit Sub in VBA

Publish Date:2025/04/16 Views:191 Category:Vba

We will look at different ways to exit a sub in VBA through examples. Using the Exit Sub Statement in VBA While using Sub in VBA, we may want to exit it or prevent it from executing further if an error occurs or the user provides wrong inpu

Calling a Sub in VBA

Publish Date:2025/04/16 Views:190 Category:Vba

We will show you how to call a sub within another sub through examples in VBA. Calling a Sub in VBA When dealing with multiple subroutines, we may encounter situations where we need to call multiple subroutines for the same function. Some f

Check if a string contains a substring in VBA

Publish Date:2025/04/16 Views:189 Category:Vba

This article demonstrates the use of Instr() the , , InstrRev() and Like functions to check whether a main string contains a substring. Use Instr() the function to check if the main string contains a substring Instr() Function syntax: InStr

Convert a string to a number in VBA

Publish Date:2025/04/16 Views:188 Category:Vba

This article will discuss how to convert a string to a number in VBA Excel. There is a step-by-step guide and many examples for us to understand. Convert a string to a number in VBA In VBA code, numbers saved as text must be converted to re

Concatenating strings in VBA

Publish Date:2025/04/16 Views:112 Category:Vba

We will cover how to concatenate strings in VBA and explain how to use Excel and operators. Concatenating strings in VBA We can concatenate multiple strings into a singular string in VBA and use the operator to separate the values ​​of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial