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

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

问题描述

由于

 暗淡ARR1为Variant
昏暗ARR2为Variant
昏暗ARR3为VariantARR1 =阵列(A,1B,2)
ARR2 =阵列(C,3,D,4)

我可以ARR1和ARR2,结果存储做ARR3什么样的操作使得:

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


解决方案

不幸的是,在VB6数组类型并没有所有的华丽堂皇的许多功能。您是pretty多将不得不刚刚经历了数组迭代,然后手动将它们插入到第三个

假定两个阵列的长度相同的

 暗淡ARR1()为Variant
昏暗的ARR2()为Variant
昏暗的ARR3()为VariantARR1()=阵列(A,1B,2)
ARR2()=阵列(C,3,D,4)使用ReDim ARR3(UBound函数(ARR1)+ UBound函数(ARR2)+1)昏暗我作为整数
对于i = 0到UBound函数(ARR1)
    ARR3(我* 2)= ARR1(I)
    ARR3(ⅰ* 2 + 1)= ARR2㈠
接下来,我

更新:修正了code。很抱歉的previous车版本。我花了几分钟搞定访问VB6编译器进行检查。

Given

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

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

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)

解决方案

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

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天全站免登陆