gpt4 book ai didi

excel - 重新调整二维数组在循环 : - Can't figure out the reason 的第二次迭代中给出错误 nº 9 "subscript out of range"

转载 作者:行者123 更新时间:2023-12-04 22:17:47 25 4
gpt4 key购买 nike

这个错误让我发疯。
为了以防万一,我遵循了所有教程,但我无法弄清楚我的代码在我重新调整循环内数组的行中给出错误 nº9 的原因。
它发生在循环的第二轮。
这是代码:

   Dim ar() As Variant
Dim cont As Integer
'Here goes some SQL and querying that works and does not matter for this post. The results are returned well.

cont = 0
Do Until rs.EOF
resul = rs!f1 & " " & rs!f2
'Error thrown in the second round of the loop in the following line:
ReDim Preserve ar(cont, 1)
ar(cont, 0) = rs!ID
ar(cont, 1) = resul
rs.MoveNext
cont = cont + 1
Loop
我真的无法弄清楚为什么会发生错误。
就教程所说的而言,语法还可以。
我想创建这个二维数组,以便我可以将它附加到用户窗体中的组合框。列表。
同样,返回的记录集是好的,问题似乎是某个时候的数组。

最佳答案

来自 MS documentation for ReDim statement .

If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.


我无法重现您的错误,但相信它与上述有关。

关于excel - 重新调整二维数组在循环 : - Can't figure out the reason 的第二次迭代中给出错误 nº 9 "subscript out of range",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67244108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com