Excel VBA-将多个数组合并为一个 [英] Excel vba - combine multiple arrays into one

查看:658
本文介绍了Excel VBA-将多个数组合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有5个维数相同的数组,可以将它们组合成具有5个子数组的一个数组.

If I have 5 arrays that have same dimension, can I combine them into one array with 5 subarrays.

va = ws1.Range("A2", ws1.Cells(Rows.Count, "A").End(xlUp)).Value   
vb = ws1.Range("D2", ws1.Cells(Rows.Count, "D").End(xlUp)).Value    
vc = ws1.Range("F2", ws1.Cells(Rows.Count, "F").End(xlUp)).Value    
vd = ws1.Range("C2", ws1.Cells(Rows.Count, "C").End(xlUp)).Value    
ve = ws1.Range("E2", ws1.Cells(Rows.Count, "E").End(xlUp)).Value

我可以这样做吗?

ReDim vx(1 To UBound(va, 1), 1 To 5, 1 To 1)
vx(1,1,1) = va
vx(1,2,1) = vb
vx(1,3,1) = vc
vx(1,4,1) = vd
vx(1,5,1) = ve

还是我必须一步一步地做到这一点?

Or do I have to do it one element by one?

推荐答案

将数组合并为数组通常称为锯齿数组(如何在VBA中设置锯齿状阵列"?

Combining arrays into array is named usually jagged array (info for C# jagged arrays and How do I set up a "jagged array" in VBA? and Can you declare jagged arrays in excel VBA directly?), if the number of elements is not the same. In the case below va has one element more than vb and vc:

Option Explicit

Public Sub TestMe()

    Dim va, vb, vc, vx

    va = Array(1, 2, 3, 4)
    vb = Array(11, 22, 33)
    vc = Array(111, 222, 333)

    ReDim vx(3)
    vx(0) = va
    vx(1) = vb
    vx(2) = vc

    Debug.Print vx(0)(1)
    Debug.Print vx(0)(2)

    Debug.Print vx(1)(1)
    Debug.Print vx(2)(2)

End Sub

您只需声明vx,然后按原样为其分配vavbvc.然后访问这样的元素-> vx(1)(0)

You simply declare vx and you assign va, vb, vc to it as they are. Then you access the elements like this -> vx(1)(0)

这篇关于Excel VBA-将多个数组合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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