填充VBA动态数组 [英] Populating VBA dynamic arrays

查看:77
本文介绍了填充VBA动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码为我提供了错误9下标超出范围".我的意思是声明一个动态数组,以便在向其中添加元素时尺寸发生变化.我需要像在JS中那样在其中存储某些东西之前,必须在该数组上创建一个斑点"吗?

The following code gives me error 9 "subscript out of range". I meant to declare a dynamic array so that the dimension changes as I add elements to it. Do I have to create a "spot" on the array before I store something in it like in JS?

Sub test_array()
    Dim test() As Integer
    Dim i As Integer
    For i = 0 To 3
        test(i) = 3 + i
    Next i
End Sub

推荐答案

在您的for循环中,对数组使用Redim,如下所示:

in your for loop use a Redim on the array like here:

For i = 0 to 3
  ReDim Preserve test(i)
  test(i) = 3 + i
Next i

这篇关于填充VBA动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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