本文介绍了“下标超出范围"在VBA阵列上调用LBound()或UBound()时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码产生错误下标超出范围",我不知道为什么.有人可以解释吗?

The following code produces the error "Subscript out of range" and I do not know why. Can someone please explain?

    Dim errorc As Integer
    Dim myarray() As Variant


    errorc = 1

    If Len(Me.txt_Listnum) = 0 Then
    ReDim Preserve myarray(errorc)
    myarray(errorc) = "Numer Listy"
    errorc = errorc + 1
    End If

    If Len(Me.cbo_ByWho) = 0 Then
    ReDim Preserve myarray(errorc)
    myarray(errorc) = "Wystawione przez"
    errorc = errorc + 1
    End If

    If Len(Me.cbo_ForWho) = 0 Then
    ReDim Preserve myarray(errorc)
    myarray(errorc) = "Wystawione na"
    errorc = errorc + 1
    End If

    For i = LBound(myarray) To UBound(myarray)
        msg = msg & myarray(i) & vbNewLine
    Next i

    If errorc > 0 Then
       MsgBox "da" & msg

    End If

推荐答案

如果所有表单控件都已填充,则您的代码将失败,因此myarray绝不会得到ReDim.对于统一化的动态数组,

Your code will fail if all of the form controls are populated and therefore myarray never gets ReDim'd. For an unititialized dynamic array,

Dim myarray() As Variant

(即,随后未使用ReDim调整大小的对象),对其调用LBound()UBound()将会失败,并显示下标超出范围".

(i.e., one that has not been subsequently sized with ReDim), calling LBound() or UBound() on it will fail with "Subscript out of range."

这篇关于“下标超出范围"在VBA阵列上调用LBound()或UBound()时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:10