简体   繁体   English

VBA下标超出范围错误?

[英]Subscript out of range error in VBA?

I am trying to keep the values in the array. 我试图将值保留在数组中。 There is some 604 values it retrieves. 它检索到一些604个值。 This is giving me subscript out of range error. 这给我下标超出范围的错误。 Can anyone help? 有人可以帮忙吗?

PlCounter = 1    
ReDim PlArray(1 To PlCounter)

For Each plv In fs.PickListValues
    Debug.Print "entered into loop"

    Set pl = plv

    Debug.Print pl.Value

    If Len(pl.Value) = 0 Then
        Debug.Print " The length is null ..so assigining null"

        ReDim Preserve PlArray(1 To PlCounter)

        PlArray(PlCounter) = "NULL"
        PlCounter = PlCounter + 1
    Else
        Debug.Print " The length is not null ..so assigining vlaues"

        ReDim Preserve PlArray(1 To PlCounter)

        PlArray(PlCounter) = pl.Value
        PlCounter = PlCounter + 1
    End If

    Next plv
    End If
Next v1

Debug.Print "The final value of Plcoutner is "; PlCounter
Debug.Print "The Final Value of PlArray "; PlArray(PlCounter - 1) -- This is getting out of range error

I believe that you are trying to print PlArray(PlCounter - 1) when in fact your array goes from 1 to PlCounter, so in essence the debug print is trying to print PlArray(0) which is out of range. 我相信您实际上是在将数组从1变为PlCounter时尝试打印PlArray(PlCounter-1),因此从本质上讲,调试打印正在尝试打印超出范围的PlArray(0)。

You could fix this by replacing this line: 您可以通过替换以下行来解决此问题:

 Debug.Print "The Final Value of PlArray "; PlArray(PlCounter - 1)

With something like this: 用这样的东西:

 If PlCounter > 1 then Debug.Print "The Final Value of PlArray "; PlArray(PlCounter - 1)

If all you are trying to get out of the array is the upper-most value (as in, the value at the upper-most bound) then just use the property meant for that: 如果您要尝试离开数组的全部是最高值(例如,在最高界限处的值),则只需使用用于此目的的属性即可:

Debug.Print "The upper bound is "; Ubound(PlArray); "with a value of "; PlArray(Ubound(PlArray))

This ensures that you get the very last index of the array, regardless of how it is defined. 这样可以确保获得数组的最后一个索引,而不管其定义如何。 This will also work if there is only one item in the array. 如果数组中只有一项,这也将起作用。

Likewise, you could use a similar operation when using Redim : 同样,使用Redim时可以使用类似的操作:

ReDim Preserve PlArray(LBound(PlArray) To UBound(PlArray) + 1)

This will help you avoid using that counter variable which will inevitably cause issues, especially since it is only being used to resize the array. 这将帮助您避免使用将不可避免地引起问题的计数器变量,尤其是因为它用于调整数组大小时。

On a separate note, you may want to consider loading your range into an array in one shot. 另外,您可能要考虑一次将范围加载到数组中。 This will be faster to loop through as well (if you want to nullify what would otherwise be Empty for null cells).: 这将是更快的遍历以及(如果你想否认原本是Empty的空细胞):

Dim Foo as Variant
Foo = SomeWorksheet.Range("A1:A100").Value

Keep in mind this will create a 2d array with a lower bound of 1 on both dimensions. 请记住,这将创建一个二维数组,两个维度的下限均为1 So, if you need a 1d array, you must translate the items out of this array and into your 1d array. 因此,如果需要一维数组,则必须将项目从该数组中转换出来并转换为一维数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM