Redim Preserve使'下标超出范围'

问题描述 投票:2回答:2

我想Redim Preserve一个数组我一直得到错误'下标超出范围'。我知道只有最后一个维度的大小才能改变。这正是我在做的事情。这里出了什么问题?数组的类型是Variant

BmMatrix = Sheets("BENCH").Range("a60", ActiveSheet.Range("a60").End(xlDown).End(xlToRight))
'totaal gewicht per subdeel in array wegschrijven
Dim aBmMatrix()
aBmMatrix = BmMatrix
rij = UBound(BmMatrix, 1)
kol = UBound(BmMatrix, 2) + 1
ReDim Preserve aBmMatrix(rij, kol)
TotGewKol = UBound(aBmMatrix, 2)
For i = 2 To UBound(BmMatrix, 1)
    g = 0 'g wordt totaal gewicht van land bv
    If BmMatrix(i, bm_kolom) <> "x" Then
        For j = 2 To UBound(bmexnul, 1)
            If bmexnul(j, weightkolom) = BmMatrix(i, bm_kolom) Then g = g + bmexnul(j, 10)
        Next j
    End If
    aBmMatrix(i, TotGewKol) = g
    aBmMatrix(1, TotGewKol) = "Totaal gewicht" 'titel kolom
Next i
arrays excel vba preserve
2个回答
3
投票

因为您使用范围的aBmMatrix属性分配Value数组,所返回的数组的每个维度的下限为1

当您稍后在不明确提供下限的情况下对其进行重新设置时,redim会尝试为每个维分配默认的下限,即0

您需要明确提供下限:

ReDim Preserve aBmMatrix(lbound(aBmMatrix,1) to rij, lbound(aBmMatrix,2) to kol)

0
投票

只更改数组的最后一个维度时,只能使用保留。

© www.soinside.com 2019 - 2024. All rights reserved.