Declare and Initialize String Array in VBA

ArraysVbaInitialization

Arrays Problem Overview


This should work according to another stack overflow post but its not:

Dim arrWsNames As String() = {"Value1", "Value2"}

Can anyone let me know what is wrong?

Arrays Solutions


Solution 1 - Arrays

Try this:

Dim myarray As Variant
myarray = Array("Cat", "Dog", "Rabbit")

Solution 2 - Arrays

In the specific case of a String array you could initialize the array using the Split Function as it returns a String array rather than a Variant array:

Dim arrWsNames() As String
arrWsNames = Split("Value1,Value2,Value3", ",")

This allows you to avoid using the Variant data type and preserve the desired type for arrWsNames.

Solution 3 - Arrays

The problem here is that the length of your array is undefined, and this confuses VBA if the array is explicitly defined as a string. Variants, however, seem to be able to resize as needed (because they hog a bunch of memory, and people generally avoid them for a bunch of reasons).

The following code works just fine, but it's a bit manual compared to some of the other languages out there:

Dim SomeArray(3) As String

SomeArray(0) = "Zero"
SomeArray(1) = "One"
SomeArray(2) = "Two"
SomeArray(3) = "Three"

Solution 4 - Arrays

Dim myStringArray() As String
*code*
redim myStringArray(size_of_your_array)

Then you can do something static like this:

myStringArray = { item_1, item_2, ... }

Or something iterative like this:

Dim x
For x = 0 To size_of_your_array
    myStringArray(x) = data_source(x).Name
Next x

Solution 5 - Arrays

Public Function _
CreateTextArrayFromSourceTexts(ParamArray SourceTexts() As Variant) As String()

    ReDim TargetTextArray(0 To UBound(SourceTexts)) As String
    
    For SourceTextsCellNumber = 0 To UBound(SourceTexts)
        TargetTextArray(SourceTextsCellNumber) = SourceTexts(SourceTextsCellNumber)
    Next SourceTextsCellNumber

    CreateTextArrayFromSourceTexts = TargetTextArray
End Function

Example:

Dim TT() As String
TT = CreateTextArrayFromSourceTexts("hi", "bye", "hi", "bcd", "bYe")

Result:

TT(0)="hi"
TT(1)="bye"
TT(2)="hi"
TT(3)="bcd"
TT(4)="bYe"

Enjoy!

Edit: I removed the duplicatedtexts deleting feature and made the code smaller and easier to use.

Solution 6 - Arrays

An only-what's-needed function that works just like array() but gives a string type. You have to first dim the array as string, as shown below:

Sub UseStringArray()

    Dim sample() As String
    sample = StringArray("dog", "cat", "horse")

End Sub

Function StringArray(ParamArray ArgList())

    ReDim tempArray(UBound(ArgList)) As String
    For i = 0 To UBound(ArgList)
        tempArray(i) = ArgList(i)
    Next
    StringArray = tempArray

End Function

For more on converting array types see here: https://stackoverflow.com/questions/66907468/how-transform-variant-to-double-format-and-vice-versa-in-vba

Solution 7 - Arrays

Using

Dim myarray As Variant

works but

Dim myarray As String

doesn't so I sitck to Variant

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKairanView Question on Stackoverflow
Solution 1 - ArraysEldar AgalarovView Answer on Stackoverflow
Solution 2 - ArraysAikenView Answer on Stackoverflow
Solution 3 - ArraysDavid WilsonView Answer on Stackoverflow
Solution 4 - ArraysAndrew SlentzView Answer on Stackoverflow
Solution 5 - Arraysmatan justmeView Answer on Stackoverflow
Solution 6 - ArraysMark E.View Answer on Stackoverflow
Solution 7 - ArraysKeith KennyView Answer on Stackoverflow