如何在Excel VBA中合并两个没有循环的数组?

时间:2016-12-26 06:41:20

标签: excel-vba vba excel

我在Excel VBA中有两个数组:

myArr1 = Array("a", "b", "c")
myArr2 = Array("1", "2", "3")

现在我想要数组myArr3,虚拟代码是myArr3 = myArr1 & myArr2,因此myArr3的内容是(" a1"," b2",& #34; C3"。)

有人能教我如何在没有循环功能的情况下实现这一步吗?

1 个答案:

答案 0 :(得分:1)

以下将在没有(显式)循环的情况下进行,但是这样做不是一个好主意

myArr1 = Array("a", "b", "c")
myArr2 = Array("1", "2", "3")
Range("A1:A3").Value = Application.Transpose(myArr1)
Range("B1:B3").Value = Application.Transpose(myArr2)
Range("C1:C3").FormulaR1C1 = "=RC[-2]&RC[-1]"
myArr3 = Application.Transpose(Range("C1:C3").Value)

注意:由于它们的创建方式,myArr1myArr2的尺寸标注为0 To 2,但myArr3的尺寸标注为1 To 3

一个不需要循环的更简单的方法是:

myArr1 = Array("a", "b", "c")
myArr2 = Array("1", "2", "3")
Dim myArr3(0 To 2)
myArr3(0) = myArr1(0) & myArr2(0)
myArr3(1) = myArr1(1) & myArr2(1)
myArr3(2) = myArr1(2) & myArr2(2)