我在VBA中有一个用户定义的函数,需要返回超过65,536个结果作为数组。但是,尝试返回超过65,536的结果为#VALUE:
Function TestSize()
Dim arr() As String
ReDim Preserve arr(1 To 200,000) ' No problem creating and using array with > 65,536 elements
' Populate array....
TestSize = arr() ' Fails here, produces #VALUE, if array > 65,536
End Function
欣赏这是一个长期存在的问题。有谁知道一项工作?
另外:如果我在VB中编写函数,我会遇到同样的问题吗?
答案 0 :(得分:0)
将结果分解为4个数组并返回包含4个项目的数组。第一个代码用于说明拆分,第二个代码可能更接近您实际想要的方式(但更复杂):
Dim arr() as string, arrP1(1 to 50000) as string, arrP2(1 to 50000) as string, arrP3(1 to 50000) as string, arrP4(1 to 50000) as string
ReDim Preserve arr(1 To 200,000)
' Populate array....
'Break results into 4 array parts:
for a=1 to 50000
arrP1(a) = arr(a)
next
for a=1 to 50000
arrP2(a) = arr(50000+a)
next
for a=1 to 50000
arrP3(a) = arr(100000+a)
next
for a=1 to 50000
arrP4(a) = arr(150000+a)
next
'Finally, return a 4-item array containing the parts:
TestSize = Array(arrP1,arrP2,arrP3,arrP4)
如果arr()可以是任何大小,那么,你可以使用多维数组,使用这样一个聪明的东西来完成上述操作。未测试的:
redim preserve arrParts(ubound(arr())\50000, 50000)
for a=1 to ubound(arr()) step 50000
for aa=1 to 50000
arrParts(a\50000, a mod 50000) = arr(a)
next
next