如何在 VBA 中将二维数组的一维分配给新的一维数组 [英] How to assign one dimension of a 2d array to a new 1d array in VBA

查看:73
本文介绍了如何在 VBA 中将二维数组的一维分配给新的一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够创建一个新数组并将其分配给 another2darray(atsomeelement)

I need to be able to create a newarray and assign it to another2darray(atsomeelement)

示例

array1(0) = 1
array1(1) = 2

现在

array2(0) = array1

因此

array2(0)(0) = 1
array2(0)(1) = 2

现在我想创建一个新数组并将array2的1d分配给它.

Now I want to take make a new array and assign 1d of the array2 to it.

newarray = array2(0)

因此

newarray(0) = 1
newarray(1) = 1

我无法在 VBA 代码中执行此操作.
下面的代码片段,如果您注释掉我尝试将 array2(1) 分配给 arraynew 的最后一部分.

I cannot do this in VBA code.
Code snippet below, works if you comment out the last section where I try and assign array2(1) to arraynew.

Function test()
    Dim array1(0 To 20) As String
    Dim array2(0 To 5) As Variant
    Dim count As Integer

    For count = 0 To UBound(array1)
     array1(count) = count
    Next count

    'now that array1 is filled i want to insert it into array2(1)
    array2(1) = array1

    ' test
    MsgBox (array2(1)(3))

    'now i want to create a new string array and assign it to array2(1)
    Dim arraynew(0 To 20) As String
    arraynew = array2(1)
    'this is what fails. 

End Function

推荐答案

不能分配给固定大小的数组.将其声明为动态的.

You cannot assign to a fixed-size array. Declare it as a dynamic one.

Dim arraynew() As String
arraynew = array2(1) 

这篇关于如何在 VBA 中将二维数组的一维分配给新的一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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