在 VBA 中声明和初始化字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19369132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:54:23  来源:igfitidea点击:

Declare and Initialize String Array in VBA

arraysvbainitialization

提问by Kairan

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?

任何人都可以让我知道出了什么问题吗?

回答by Eldar Agalarov

Try this:

尝试这个:

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

回答by Aiken

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

在 String 数组的特定情况下,您可以使用Split 函数初始化该数组,因为它返回一个 String 数组而不是 Variant 数组:

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.

这允许您避免使用 Variant 数据类型并为 arrWsNames 保留所需的类型。

回答by David Wilson

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).

这里的问题是数组的长度未定义,如果数组明确定义为字符串,这会使 VBA 感到困惑。然而,变体似乎能够根据需要调整大小(因为它们占用了大量内存,人们通常出于各种原因避免使用它们)。

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"

回答by Andrew Slentz

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

回答by matan justme

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.

编辑:我删除了重复文本删除功能,并使代码更小且更易于使用。

回答by Keith Kenny

Using

使用

Dim myarray As Variant

works but

有效但

Dim myarray As String

doesn't so I sitck to Variant

不是,所以我选择了 Variant