【问题标题】:subscript out of range when increasing array size增加数组大小时下标超出范围
【发布时间】:2018-01-04 17:44:38
【问题描述】:

我正在创建一个用户表单,它可以在数组中获取尽可能多的输入。每次输入新值时,数组都会变大 进入第二个输入时,下标超出范围

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

【问题讨论】:

    标签: vba range out


    【解决方案1】:

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

    ReDim Preserve flows(UBound(flows) + 1)
    

    【讨论】:

    • 来自审核队列:我可以请求您在您的答案周围添加更多上下文。仅代码的答案很难理解。如果您可以在帖子中添加更多信息,它将帮助提问者和未来的读者。另请参阅Explaining entirely code-based answers
    猜你喜欢
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多