在我的 VBA 程序中,我有一个很大的数据数组,我需要不断地使用它的子数组。
我的方法是:
Redim subArr(rowBegin to rowEnd)
For r = rowBegin to rowEnd
subArr(r) = bigArr(r)
Next r
Run Code Online (Sandbox Code Playgroud)
请问有没有更有效的方法来引用这种子数组?谢谢...
使用数组的速度非常快,因此这可能不会带来明显的好处 - 尽管我可以理解它从编码意义上比循环填充较小的数组更有吸引力
鉴于您正在使用单个元素数组,您可以:
Join将带有分隔符的大数组转换为单个字符串Split通过“标记”字符串将大数组分成大数组,然后使用分隔符将缩减后的字符串分隔成较小的数组下面的代码将数字 1 到 100 转储到一个数组中,然后按照上面的方式拆分它以提取前 10 条记录
Sub test()
Dim bigArr
Dim subArr
Dim strSep As String
Dim strDelim As String
Dim strNew As String
Dim rowBegin As Long
Dim rowEnd As Long
strDelim = ","
strSep = "||"
'fill array with 1 to 100
bigArr = Application.Transpose(Application.Evaluate("row(1:100)"))
rowBegin = 1
rowEnd = 10
bigArr(rowEnd + 1) = strSep
'make a single string
strNew = Join(bigArr, strDelim)
'split the string at the marker
vArr = Split(strNew, strSep)
ReDim subArr(rowBegin To rowEnd)
'split the smaller string with the desired records
subArr = Split(Left$(vArr(0), Len(vArr(0)) - 1), strDelim)
End Sub
Run Code Online (Sandbox Code Playgroud)