添加到Excel中VBA数组功能 [英] Adding to an Array function in Excel VBA

查看:204
本文介绍了添加到Excel中VBA数组功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个数组中加入双数组的数组的循环。这里是code我有:

I am trying to add an array to an array of Double arrays in a for loop. Here is the code I have:

Sub Test3()
 Dim a() As Double, i As Integer
 ReDim a(1 To 10, 1 To 3)

 Dim d

 For i = 1 To 3
  d = Array(a)
 Next i

End Sub

在这个测试我只是想补充的3份A到D。我有d =阵列(一)这当然是不行的,但我不知道什么是符合

In this test I'm just trying to add 3 copies of 'a' into 'd'. I have d = Array(a) which of course doesn't work, but I don't know what line to replace it with

编辑code为清楚起见

Edited code for clarity

新的code尝试:

Sub Test3()
 Dim a() As Double, i As Integer
 ReDim a(1 To 10, 1 To 3)
 a(1, 2) = 3.5

 Dim d() As Variant

 For i = 1 To 3
  ReDim Preserve d(1 To i)
  d(i) = Array(a)
 Next i


 Dim x() As Double
 x = d(1)   ' Error, Type Mismatch

 MsgBox (x(1, 2))

End Sub

我得到一个类型不匹配的错误在X = D(1)

I get the error of a type mismatch on x = d(1)

推荐答案

您想要什么叫做阵列的铁血阵列或阵列

You want what's called a "Jagged Array", or Array of Arrays

试试这个

Sub Demo()
    Dim a() As Double, b() As Double, c() As Double, i As Integer

    ReDim a(1 To 10, 1 To 3)
    ReDim b(1 To 2, 1 To 4)
    ReDim c(1 To 5, 1 To 11)

    a(1, 2) = 3.5
    b(1, 2) = 2.5

    Dim d As Variant
    ReDim d(1 To 6)

    ' Add 3 copies of a to d
    For i = 1 To 3
     d(i) = a
    Next i

    ' add other arrays to d
    d(4) = b
    d(5) = c

    ' Access elements of d
    Dim x() As Double
    x = d(1)

    MsgBox x(1, 2)
    MsgBox d(1)(1, 2)
    MsgBox d(4)(1, 2)
End Sub

这篇关于添加到Excel中VBA数组功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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