给定

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中,使得:

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

最佳答案

不幸的是,VB6中的Array类型并没有那么多功能。您将几乎不得不遍历数组并将其手动插入到第三个数组中。

假设两个数组的长度相同

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编译器进行检查。

关于arrays - 如何在VBA中合并两个阵列?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1588913/

10-12 17:32