REDIM与第二维现有阵列? [英] ReDim existing array with a second dimension?

查看:178
本文介绍了REDIM与第二维现有阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我宣布我的VBA函数的数组具有动态的大小。正如我不能REDIM二维或更多维数组的第一个维度,我可以添加第二个维度,一组阵列?

I declared an array in my VBA function that has a dynamic size. As I cannot ReDim the first dimension of a two- or more-dimensional array, can I add a second dimension to a set array?

这是我如何动态地设置我的数组的大小。

This is how I dynamically set the size of my array.

Dim nameArray() As String
Dim arrayCount As Long

For i = 1 To 100
    ReDim Preserve nameArray(1 to arrayCount)
    nameArray(arrayCount) = "Hello World"
    arrayCount = arrayCount + 1
Next i

现在我想补充的第二个维度。

Now I would like to add a second dimension.

ReDim Preserve nameArray(1 To arrayCount, 1 To 5)

不起作用。

有没有解决办法?

推荐答案

有没有任何内置的方式来做到这一点。只要创建一个新的二维数组和您现有的一维数组中的内容转移到新阵列的第一行。

There isn't any built-in way to do this. Just create a new two-dimensional array and transfer the contents of your existing one-dimensional array into the first row of that new array.

这是这个函数做什么:

Function AddDimension(arr() As String, newLBound As Long, NewUBound As Long) As String()
    Dim i As Long
    Dim arrOut() As String
    ReDim arrOut(LBound(arr) To UBound(arr), newLBound To NewUBound)
    For i = LBound(arr) To UBound(arr)
        arrOut(i, newLBound) = arr(i)
    Next i
    AddDimension = arrOut
End Function

实例:

nameArray = AddDimension(nameArray, 1, 5)

这篇关于REDIM与第二维现有阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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