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

查看:35
本文介绍了填充 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天全站免登陆