如何在 VBA 中合并两个数组? [英] How do I Merge two Arrays in VBA?

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

问题描述

给定

Dim arr1 As Variant
Dim arr2 As Variant
Dim arr3 As Variant

arr1 = Array("A", 1, "B", 2)
arr2 = Array("C", 3, "D", 4)

我可以对 arr1 和 arr2 进行什么样的操作并将结果存储在 arr3 中使得:

What kind of operations can I do on arr1 and arr2 and store result in arr3 such that:

arr3 = ("A", "C", 1, 3, "B", "D", 2, 4)

推荐答案

不幸的是,VB6 中的 Array 类型并没有那么多令人眼花缭乱的特性.您几乎必须遍历数组并手动将它们插入第三个

Unfortunately, the Array type in VB6 didn't have all that many razzmatazz features. You are pretty much going to have to just iterate through the arrays and insert them manually into the third

假设两个数组的长度相同

Assuming both arrays are of the same length

Dim arr1() As Variant
Dim arr2() As Variant
Dim arr3() As Variant

arr1() = Array("A", 1, "B", 2)
arr2() = Array("C", 3, "D", 4)

ReDim arr3(UBound(arr1) + UBound(arr2) + 1)

Dim i As Integer
For i = 0 To UBound(arr1)
    arr3(i * 2) = arr1(i)
    arr3(i * 2 + 1) = arr2(i)
Next i

更新:修复了代码.很抱歉之前的错误版本.我花了几分钟访问 VB6 编译器来检查它.

Updated: Fixed the code. Sorry about the previous buggy version. Took me a few minutes to get access to a VB6 compiler to check it.

这篇关于如何在 VBA 中合并两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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