vba 如何将一组文本值分配给字符串数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/507653/
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 09:36:34  来源:igfitidea点击:

How to assign a set of text values to a string array?

arraysexcelvba

提问by Chris Gunner

How can I assign a set of text values to an array? Nothing I tried is working!

如何将一组文本值分配给数组?我试过的任何东西都不起作用!

Months = Array("Jan", "Feb", ..., "Dec")

and others I tried do not work!

我试过的其他人不起作用!

回答by Stephen

Here's something about VB: http://www.devx.com/vb2themax/Tip/18322

这是关于 VB 的一些内容:http: //www.devx.com/vb2themax/Tip/18322

Visual Basic doesn't provide any way to declare an array and initialize its elements at the same time. In most cases you end up with setting individual elements one by one, as in:

Visual Basic 不提供任何方法来声明数组并同时初始化其元素。在大多数情况下,您最终会一个一个地设置单个元素,如下所示:

  Dim strArray(0 To 3) As String
  strArray(0) = "Spring" 
  strArray(1) = "Summer"
  strArray(2) = "Fall"
  strArray(3) = "Winter"

Under VB4, VB5, and VB6 you can create an array of Variants on the fly, using the Array() function:

在 VB4、VB5 和 VB6 下,您可以使用 Array() 函数动态创建变体数组:

  Dim varArray() As Variant 
  varArray() = Array("Spring", "Summer", "Fall", "Winter")

but there is no similar function to create arrays of data types other than Variant. If you're using VB6, however, you can create String arrays using the Split() function:

但是没有类似的函数来创建 Variant 以外的数据类型数组。但是,如果您使用的是 VB6,则可以使用 Split() 函数创建字符串数组:

  Dim varArray() As String 
  ' arrays returned by Split are always zero-based 
  varArray() = Split("Spring;Summer;Fall;Winter", ";")

回答by Mark Biek

I'm pretty sure you can only do it like this:

我很确定你只能这样做:

 dim months(2) as string

 months(0) = "Jan"
 months(1) = "Feb"
 months(2) = "Mar"

回答by Seth Reno

If you're talking about vbscript then this works:

如果你在谈论 vbscript 那么这有效:

months = Array("may","june","july")

If it's vb.net then:

如果是 vb.net 那么:

dim months() as string = {"may","june","july"}