简体   繁体   English

使用ReDim(VBA)超出范围的下标

[英]Subscript out of Range with ReDim (VBA)

Could you please explain to me why this simple VBA code fails at the last line ("Subscript out of Range"): 你能否向我解释为什么这个简单的VBA代码在最后一行失败(“下标超出范围”):

    Dim test() As Variant
    ReDim test(0, 1)
    test(0,0) = "key"
    test(0,1) = 1
    ReDim Preserve test(1, 1)

Resizing with Preserve. 使用Preserve调整大小。 If you use Preserve, you can resize only the last dimension of the array, and for every other dimension you must specify the same bound it already has in the existing array. 如果使用“保留”,则只能调整数组的最后一个维度,并且对于每个其他维度,必须指定现有数组中已有的相同边界。

For example, if your array has only one dimension, you can resize that dimension and still preserve all the contents of the array, because you are changing the last and only dimension. 例如,如果您的数组只有一个维度,则可以调整该维度的大小并保留数组的所有内容,因为您要更改最后一个维度。 However, if your array has two or more dimensions, you can change the size of only the last dimension if you use Preserve. 但是,如果您的数组具有两个或更多维度,则在使用“保留”时,可以仅更改最后一个维度的大小。

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

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