使用变量字符串引用数组名称的VBS给出类型不匹配'ubound'代码:800A000D

时间:2018-05-02 18:54:24

标签: arrays variables vbscript

我有40个阵列,例如 arrOne arrTwo arrThree 等等。所以:

Dim arrOne

arrOne = Array("word","Cat","Dog","Bob")

' I can lbound and ubound this array... **BUT!!!**

strChooseOne = "arrOne"

For I = 0 to ubound(strChooseOne)

' ***do stuff***

Next

这会导致错误:

  

类型不匹配'ubound'
  代码:800A000D

可能因为strChooseOne是一个包含“arrOne”的字符串变量,而arrOne是一个实际的数组......

我需要循环浏览500个项目,这些项目将以任意顺序引用40个阵列中的1个...我宁愿不必复制' 做东西 代码40次,检查每个数组...... 我想使用For ... Next循环代码一次,每次更改数组的名称......

我用Google搜索了错误代码,但似乎没什么关系。

1 个答案:

答案 0 :(得分:0)

看一下下面的例子:

Option Explicit

Dim aData
Dim aTmp
Dim i
Dim j
Dim s

' Populate main array with 40 nested arrays of random elements quantity
Randomize
ReDim aData(39)
For i = 0 To 39
    aTmp = Array()
    Do
        ReDim Preserve aTmp(UBound(aTmp) + 1)
        aTmp(UBound(aTmp)) = "[" & Right("0" & i, 2) & Chr(65 + UBound(aTmp)) & "]"
    Loop Until Rnd > 0.55
    aData(i) = aTmp
Next

' Make processing you need
s = ""
For i = 0 To 39
    For j = 0 To UBound(aData(i))
        ' ***do stuff***
        s = s & aData(i)(j)
    Next
    s = s & vbCrLf
Next

WScript.Echo s