为什么ReDim会将下标超出范围错误9

时间:2014-11-26 16:55:40

标签: excel-vba dynamic-arrays excel-2013 vba excel

我的代码在VB中动态创建一个数组 公共函数getIndvidualCuts(dblsawKerf As Double)当Double()'返回并且所有切割的数组

Dim intQuantity As Integer
intQuantity = 0
Dim i As Integer

intQuantity = 0 ' an integer to store the total quanitiy
For i = 0 To Me.getNumCuts() - 1  'for each different cut
    intQuantity = intQuantity + getCutQuantity(i) 'add the cut quantity to the total quantity
Next

Dim total() As Double 'initialize the total cuts array to make room for each individual cut
ReDim total(intQuantity - 1) **error is here**

我不明白为什么ReDim会抛出错误。我一直在寻找,但我发现所有人都在使用 ReDim Preserve 的情况如果你有一个不同的动态阵列解决方案请分享

提前致谢

1 个答案:

答案 0 :(得分:1)

无效数组下标的示例将是非整数或负下标。如果程序中的任何一点intQuantity = 0,则会产生-1的数组的下标。同样,如果getCutQuantity返回非整数,那么您的数组下标将是非整数。很高兴帮助。

相关问题