如何在for循环中动态命名VBScript变量?

时间:2018-12-06 22:56:20

标签: vbscript

如何在迭代循环中动态声明并分配变量。例如:

for i = i to some_number
    dim "prefix" & i
    "prefix" & i = 2 * i
next

1 个答案:

答案 0 :(得分:0)

Dim MyArray(5)

For x = 0 to 4
    MyArray(x) = 2 * (x + 1)
Next

For each thing in MyArray
    Wscript.echo thing
Next

For x = 0 to 4
     wscript.echo MyArray(x)
Next

如果您不知道字典的数量通常比较容易。如果您有两个或两个以上的项目,那就更容易了。从帮助。 https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dictionary-object

Dim d                   'Create a variable
Set d = CreateObject(Scripting.Dictionary)
d.Add "a", "Athens"     'Add some keys and items
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

也可以从文本中制作出来。

MyArray1 = Split("This is a bunch of words", " ")    

For each thing in MyArray1
    Wscript.echo thing
Next