增加数组大小时,下标超出范围

时间:2017-07-28 15:21:43

标签: vba range out

我创建的userform在数组中尽可能多地获得输入。每次输入新值时,阵列都会变大 当到达第二个输入时,我得到下标超出范围

Sub getFlow()


Do
    thisFlow = UserForm4.TextBox1.Value
    If Val(thisFlow) >= 0 Then

        If isInitiated = True Then
            ReDim Preserve flows(1 To UBound(flows) + 1)
        Else
            ReDim flows(1)
            isInitiated = True
            check = True
        End If

            flows(UBound(flows)) = thisFlow
            UserForm4.TextBox1 = ""
                With UserForm4.TextBox1
                .SetFocus
                .SelStart = 0
                .SelLength = Len(.Text)
                End With
    Else
        MsgBox "Value should be equal or greater than zero!"
    End If

Exit Do
Loop

1 个答案:

答案 0 :(得分:0)

当你ReDim时,你不能指定下限 - 你只需要指定新的上限。

ReDim Preserve flows(UBound(flows) + 1)