标超出范围与使用ReDim(VBA)的 [英] Subscript out of Range with ReDim (VBA)

查看:403
本文介绍了标超出范围与使用ReDim(VBA)的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个简单的VBA code。在最后一行(下标超出范围)失败,能不能请你给我解释一下:

 暗淡试验(+)为Variant
    使用ReDim试验(0,1)
    试验(0,0)=钥匙
    试验(0,1)= 1
    使用ReDim preserve试验(1,1)


解决方案

与preserve调整大小。如果您使用preserve,你只能调整数组的最后一个维度,并为每其他维度您必须指定相同的约束它已经有现有的阵列中的

例如,如果你的数组只有一个尺寸,可以调整该维度,仍然preserve阵列中的所有内容,因为你正在改变最后的也是唯一的维度。但是,如果你的数组有两个或多个维度,你只能改变最后维的大小,如果您使用preserve。

Could you please explain to me why this simple VBA code fails at the last line ("Subscript out of Range"):

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

解决方案

Resizing with 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.

这篇关于标超出范围与使用ReDim(VBA)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆