我正在为Visual Basic for PowerPoint 2010编写一个宏。我想初始化一个非常大的字符串列表,如:
big_ol_array = Array( _
"string1", _
"string2", _
"string3", _
"string4" , _
.....
"string9999" _
)
...但我在编辑器中收到“Too many lines continuations”错误。当我尝试在没有换行符的情况下初始化大数组时,VB编辑器无法处理如此长的行(1000+)字符。
有没有人知道在VB中初始化大量字符串的好方法?
提前致谢!
答案 0 :(得分:3)
我认为没有办法做你想做的事。但是存在一些变通方法。
例如,您可以从文件中加载字符串列表。 这个例子可以给你一个提示:
Dim value As String = File.ReadAllText("C:\file.txt")
此外,本页还讨论了它:Excel macros - Too many line continuations。
答案 1 :(得分:1)
解决方法的一个选项可能是使用Join
命令,如下所示:
Const ARRAY_VALUES As String = _
"string1,string2," & _
"string3,string4"
Dim big_ol_array() As String
big_ol_array() = Split(ARRAY_VALUES, ",")
这将允许您在每一行上放置多个条目,然后您可以使用多行连续。
或者,如果你真的想要每行一个值,你可以使用多个常量,如下所示:
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
当然,如果它与您的数据冲突,您可以选择不同的分隔符。在这种情况下,我将使用一个非常罕见但可读的分隔符,如子弹(•
),可以通过按住Alt键并在数字键盘上键入“0149”来键入。然后你的代码看起来像这样:
Const ARRAY_VALUES As String = _
"string1•string2•" & _
"string3•string4"
Dim big_ol_array() As String
big_ol_array() = Split(ARRAY_VALUES, "•")
这是其他一些有趣的分隔符。这些都会出现在IDE中(而其他的看起来就像一个盒子):
§¸················································
答案 2 :(得分:1)
您可以将字符串值存储在外部文本文件中,并逐行将项目读入数组。我的示例(未经测试)早期绑定到 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
进一步阅读:
当然,你可能想要更灵活地调整数组的大小,当你的空间不足时,它的大小会增加一倍。
答案 3 :(得分:0)
如果你想保持简单,可以摆脱GSerg在link中发布的行继续符。实际上,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, ",")
答案 4 :(得分:-1)
Dim BigOlArray(1 to 99999) as String
BigOlArray(1) = "String1"
BigOlArray(2) = "String2"
或保存一些打字
' 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