JIYIK CN >

Current Location:Home > Learning > PROGRAM > Vba >

Declaring and Initializing String Arrays in VBA

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

In any code execution in any programming language, the need to store information is sometimes unavoidable. The good news is that VBA allows for a variety of options when it comes to storing data, one of which is arrays.

In VBA, arrays are classified based on their length and flexibility of data type.

Additionally, arrays in VBA can also be categorized based on their dimensions.

Declare and initialize a dynamic string array

The following code block will demonstrate how to create and initialize a string array.

Method 1 - Declare as Variantdata type

A dynamic array is declared by creating a variable in a variant data type. The array is then Array()initialized with a collection ( ).

Sub DynamicArrayDemo()

Dim stringArray As Variant

stringArray = Array("Lion", "Tiger", "Cheetah", "Monkey", "Elephant", "Zebra")

Debug.Print stringArray(3)

End Sub

DynamicArrayDemoOutput:

Monkey

Method 2 - Declare as a string and then use Split()the function

Declare an stringArrayarray of strings named without explicitly stating the bounds.

Sub DynamicArrayDemo()

Dim stringArray() As String
Dim str As String

str = "Lion,Tiger,Cheetah,Monkey,Elephant,Zebra"

stringArray = Split("Lion,Tiger,Cheetah,Monkey,Elephant,Zebra", ",")

Debug.Print stringArray(2)
End Sub

DynamicArrayDemoOutput:

Cheetah

Declare and initialize a static string array

The following code block demonstrates different ways to declare and initialize a static string array.

Method 1 - Declare LowerBoundand UpperBound:

Declare a static string array by explicitly declaring its first and last elements.

grammar:

Dim stringArray([LowerBound] To [UpperBound]) As String

parameter:

   
[LowerBound] The key integer referenced by the first element of the array.
[UpperBound] A key integer referencing the last element of the array.

The following example declares a stringArraystring array named with six elements, 0 through 5.

Sub StaticArrayDemo()

Dim stringArray(0 To 5) As String

stringArray(0) = "Lion"
stringArray(1) = "Tiger"
stringArray(2) = "Cheetah"
stringArray(3) = "Monkey"
stringArray(4) = "Elephant"
stringArray(5) = "Zebra"

Debug.Print stringArray(4)

End Sub

StaticArrayDemoOutput:

Elephant

Method 2 - Explicitly change下限

Lower boundDeclare a string array with a common value.

Option Base 1

Sub StaticArrayDemo()
Dim stringArray(6) As String

stringArray(1) = "Lion"
stringArray(2) = "Tiger"
stringArray(3) = "Cheetah"
stringArray(4) = "Monkey"
stringArray(5) = "Elephant"
stringArray(6) = "Zebra"

Debug.Print stringArray(1)

End Sub

StaticArrayDemoOutput:

Lion

Method 3 - Using Multidimensional Array Declaration and Initialization

In VBA, you can declare arrays with up to 60 dimensions.

grammar:

Dim stingArray( [LowerBound1] to [UpperBound1],[LowerBound2] to [UpperBound2], . . .  ) as String

parameter:

   
[LowerBound1] The key integer is the first array element referenced along the first array dimension.
[UpperBound1] The key integer is the last array element referenced along the first array dimension.
[LowerBound2] The key integer is the first array element referenced in the second array dimension.
[UpperBound2] The key integer is the last array element referenced along the second array dimension.

In the following example, a multidimensional array is declared where the first dimension is 1 to 5; then the other is 1 to 5.

Sub MultiStaticArrayDemo()

Dim stringArray(1 To 5, 1 To 5) As String
Dim i, j As Integer

For i = 1 To 5
    For j = 1 To 5
        stringArray(i, j) = "The value of (" & i & "," & j & ") is " & i * j
        Debug.Print stringArray(i, j)
    Next j
Next i

End Sub

MultiStaticArrayDemoOutput:

The value of (1,1) is 1
The value of (1,2) is 2
The value of (1,3) is 3
The value of (1,4) is 4
The value of (1,5) is 5
The value of (2,1) is 2
The value of (2,2) is 4
The value of (2,3) is 6
The value of (2,4) is 8
The value of (2,5) is 10
The value of (3,1) is 3
The value of (3,2) is 6
The value of (3,3) is 9
The value of (3,4) is 12
The value of (3,5) is 15
The value of (4,1) is 4
The value of (4,2) is 8
The value of (4,3) is 12
The value of (4,4) is 16
The value of (4,5) is 20
The value of (5,1) is 5
The value of (5,2) is 10
The value of (5,3) is 15
The value of (5,4) is 20
The value of (5,5) is 25

Previous: None

Next:Comparing Strings in VBA

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