vba 如何在 Visual Basic 中定义大的字符串列表

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

How to define large list of strings in Visual Basic

vbapowerpointpowerpoint-vba

提问by HotDogCannon

I'm writing a macro in Visual Basic for PowerPoint 2010. I'd like to initialize a really big list of strings like:

我正在为 PowerPoint 2010 在 Visual Basic 中编写一个宏。我想初始化一个非常大的字符串列表,例如:

big_ol_array = Array( _
"string1", _
"string2", _
"string3", _
"string4" , _
  .....
"string9999" _
)

...but I get the "Too many line continuations" error in the editor. When I try to just initialize the big array with no line breaks, the VB editor can't handle such a long line (1000+) characters.

...但我在编辑器中收到“太多行延续”错误。当我尝试在没有换行符的情况下初始化大数组时,VB 编辑器无法处理这么长的行 (1000+) 个字符。

Does anyone know a good way to initialize a huge list of strings in VB?

有谁知道在 VB 中初始化大量字符串的好方法吗?

Thanks in advance!

提前致谢!

采纳答案by Freelex

I don't think there's a way to do what you want. But there exist some workarounds.

我不认为有办法做你想做的事。但是存在一些解决方法。

For example, you could load your list of strings from a file. That example can show you a hint :

例如,您可以从文件加载字符串列表。该示例可以向您展示一个提示:

Dim value As String = File.ReadAllText("C:\file.txt")

Dim value As String = File.ReadAllText("C:\file.txt")

Also, this page talks about it : Excel macros - Too many line continuations.

此外,此页面还讨论了它:Excel 宏 - 太多的行延续

回答by LimaNightHawk

An option for a workaround might be to use the JoinCommand, like this:

解决方法的一个选项可能是使用Join命令,如下所示:

Const ARRAY_VALUES As String = _
"string1,string2," & _
"string3,string4"

Dim big_ol_array() As String
big_ol_array() = Split(ARRAY_VALUES, ",")

This would allow you to put multiple entries on each line, and then you could use multiple lines continuations.

这将允许您在每行上放置多个条目,然后您可以使用多行延续。

Or, if you really want one value per line, you could just use multiple constants, like this:

或者,如果您真的想要每行一个值,您可以只使用多个常量,如下所示:

Const ARRAY_VALUES1 As String = _
"string1," & _
"string2," & _
"string3," & _
"string4,"

Const ARRAY_VALUES2 As String = _
"string5," & _
"string6," & _
"string7," & _
"string8"

Const ARRAY_VALUES As String = _
ARRAY_VALUES1 & _
sARRAY_VALUES2

Of course, you could choose a different delimiter if it conflicts with your data. In cases like this I'll use a pretty rare but readable delimiter like the bullet (?), which can be typed by holding the Alt key and typing the "0149" on the number pad. Then your code would look like this:

当然,如果它与您的数据冲突,您可以选择不同的分隔符。在这种情况下,我将使用一个非常罕见但可读的分隔符,如项目符号 ( ?),可以通过按住 Alt 键并在数字键盘上输入“0149”来输入。然后你的代码看起来像这样:

Const ARRAY_VALUES As String = _
"string1?string2?" & _
"string3?string4"

Dim big_ol_array() As String
big_ol_array() = Split(ARRAY_VALUES, "?")

Here's some other interestingdelimiters. These will all show up in the IDE (while others will just look like a box):

这里有一些其他有趣的分隔符。这些都将显示在 IDE 中(而其他的看起来就像一个盒子):

§ ? · ? ¤ ? ? | ± _- ˉ ¨ a 1 2 3 ′ ° o ? ?

§ ? · ? ? ? | ± _- ˉ ¨ a 1 2 3 ′ ° o ? ?

回答by RubberDuck

To expand on freelax's answer:

要扩展freelax 的答案

You could store the string values in an external text file and read the items into an array line by line. My example (untested) early binds to the Microsoft Scripting Runtimelibrary.

您可以将字符串值存储在外部文本文件中,并将项目逐行读入数组。我的示例(未经测试)早期绑定到Microsoft Scripting Runtime库。

Dim arr() as string
Dim fso as New FileSystemObject
Dim fl as file
Dim ts as textstrean
Dim i as long ' counter

Set fl = fso.GetFile("c:\path\to\configfile.txt")
Set ts = fl.OpenAsTextStream(ForReading)
i = 0
Do While Not ts.AtEndOfStream
    Redim Preserve arr(i) 'resize the array leaving data in place
    arr(i) = ts.Readline
    i = i + 1
Loop

Further Reading:

进一步阅读:

  1. FileSystemObject
  2. File
  3. TextStream
  4. Reading a text file with vba
  5. Redim an array
  1. 文件系统对象
  2. 文件
  3. 文本流
  4. 使用 vba 读取文本文件
  5. 重新排列数组

Of course, you'll probably want to be smarter about resizing the array, doubling it's size when you run out of space.

当然,您可能希望更聪明地调整数组大小,在空间不足时将其大小加倍。

回答by L42

Get rid of the line continuation character as what GSerg posted in this linkif you want to keep it simple. Same link posted by Freelex actually.

如果您想保持简单,请删除GSerg 在此链接中发布的行继续符。实际上,Freelex 发布了相同的链接。

Dim bigStr As String, big_ol_array() As String

bigStr = "string1"
bigStr = bigStr & "," & "string2"
bigStr = bigStr & "," & "string3"
.
.
bigStr = bigStr & "," & "string9999"
big_ol_array() = Split(bigStr, ",")

回答by Steve Rindsberg

Dim BigOlArray(1 to 99999) as String
BigOlArray(1) = "String1"
BigOlArray(2) = "String2"

or to save some typing

或保存一些打字

' Keep copy/pasting the second and third lines as needed
' then all you need to change is the string
x = 1
BigOlArray(x) = "String1"
x = x+1
BigOlArray(x) = "String2"
x = x+1
BigOlArray(x) = "String3"
x = x+1