在VBA中加入两个数组?

时间:2018-12-11 15:41:57

标签: vba excel-vba

如何将这些数组与(2、4、5、3、7、6)的结果结合起来

array1 = Array(4,5,3,7,6)

array2 = Array(2)

4 个答案:

答案 0 :(得分:4)

您可以Join()并连接两个数组,然后Split()将结果返回到新数组:

array3 = Split(Join(array2, ",") & "," & Join(array1, ","), ",")

说明:
Join()将返回一个字符串,该字符串的数组(第一个参数)中的每个元素都由一个","(第二个参数)分隔。我们用另外一个逗号将这两个连接的数组连接起来,得到一个像2,4,5,3,7,6这样的字符串。然后,我们使用Split()将该字符串转换回一个数组,告诉Split()分行符是逗号","

答案 1 :(得分:3)

结合两个数组

作为Scott Craner提出的正确且可行的方法的替代方法

创建第三个数组,该数组的大小等于两个数组的总和, 然后遍历每个数组,逐个添加项目。

...我演示了一种方法

    通过循环
  • 插入 第二个数组的元素 放入主数组,而
  • 主数组仅通过Application.Index()一个内衬进行重组

由于此函数会将结果更改为基于1的数组,因此我将该数组重新分配为基于0的数组。此外,我在VBE的“即时窗口”中添加了一个可选显示,以生成2|4|5|3|7|6值:

第一步:具有与OP(插入1个元素)相同的数组值的简单演示

Sub SimpleDemo()
'[0]declare and assign zero-based 1-dimensioned arrays
    Dim main, newTop
    main = Array(4, 5, 3, 7, 6)
    newTop = Array(2)              ' only one element in a first step
'[1]transform main array by inserting(/i.e. repeating) "another" 1st element
    main = Application.Index(main, Array(1, 1, 2, 3, 4, 5)) ' changes to 1-based 1-dim array
    ReDim Preserve main(0 To UBound(main) - 1)              ' back to zero-based 1-dim array
'[2]overwrite new first element by the 1st(only) element of newTop
    main(0) = newTop(0)
'[3](optional) display in VBE's Immediate Window: main(0 To 5) ~> 2|4|5|3|7|6
    Debug.Print "main(" & LBound(main) & " To " & UBound(main) & ") ~> " & _
                Join(main, "|")
End Sub

第二步:使用AddElem过程的更通用方法

上面的演示仅插入一个元素。因此,我编写了一个AddElem过程和一个帮助功能addedElems(),以允许插入更多元素。假设所有1-dim数组都像原始文章中那样从零开始;可以轻松地顺便改一下:-)

Sub AddElem(main, newTop)
' Purp. : add/insert other array element(s) on top of zero-based main array
' Author: https://stackoverflow.com/users/6460297/t-m
' Date  : 2020-02-05
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' a)insert newTop element(s) on top of main array
    main = Application.Index(main, addedElems(main, newTop)) ' changes temporarily to 1-based mainay!
' b)make main array zero-based again (optional)
    ReDim Preserve main(0 To UBound(main) - 1)
' c)overwrite inserted starting element(s) by the newTop element(s)
    Dim i&: For i = 0 To UBound(newTop): main(i) = newTop(i): Next i
End Sub

帮助功能addedElems()

Function addedElems(main, newTop) As Variant()
'Note : help function called by AddElem()
'Purp.: return ordinal element counters of combined arrays
    Dim i&, n&: n = UBound(main) + UBound(newTop) + 1
    ReDim tmp(0 To n)
    For i = 0 To UBound(newTop): tmp(i) = i: Next i
    For i = i To n: tmp(i) = i - UBound(newTop): Next i
    addedElems = tmp        ' return combined elem counters,  e.g. Array(1,2, 1,2,3,4,5)
End Function

示例呼叫

我稍微修改了OP的第二个数组的值(Array(2)〜> Array(20,21),以演示更多元素的插入,因此 导致合并后的Array(20,21,2,4,5,3,7,6)

Sub ExampleCall()
'[0]declare and assign zero-based 1-dimensional arrays
    Dim main, newTop
    main = Array(4, 5, 3, 7, 6)
    newTop = Array(20, 21)
'[1]Add/Insert newTop on top of main array
    AddElem main:=main, newTop:=newTop     ' or simply: AddElem main, newTop

'[2](optional) display in VBE's Immediate Window: ~~> main(0 To 6) ...20|21|4|5|3|7|6
    Debug.Print "main(" & LBound(main) & " To " & UBound(main) & ") ..." & _
                Join(main, "|")
End Sub

相关链接

类似地,您可以研究应用于Insert first column in datafield array without loops or API calls的2维数组的Application.Index()函数的一些特殊性

答案 2 :(得分:1)

您可以使用arrayLists。如果需要,这也提供了一种简单的排序方式。

Option Explicit
Public Sub test()
    Dim list1 As Object, list2 As Object

    Set list1 = CreateObject("System.Collections.Arraylist")
    Set list2 = CreateObject("System.Collections.Arraylist")
    list1.Add 4
    list1.Add 5
    list1.Add 3
    list1.Add 7
    list1.Add 6
    list2.Add 2
    list1.addRange list2
    list1.Sort
End Sub

答案 3 :(得分:1)

晚了聚会,但我还要加两分钱

您可以简单地将两个阵列之一 复制 到一个新阵列中。然后Redim Preserve等于两个原始数组的大小,然后循环 第一个数组。以下代码是基本代码,但是可以快速完成工作而无需转换任何数据类型:

Sub Test()

Dim arr1 As Variant: arr1 = Array(4, 5, 3, 7, 6)
Dim arr2 As Variant: arr2 = Array(2)
Dim arr3 As Variant: arr3 = arr2

ReDim Preserve arr3(UBound(arr1) + Ubound(arr2) + 1)
For x = (UBound(arr3) - UBound(arr1)) To UBound(arr3)
    arr3(x) = arr1(x - UBound(arr2) - 1)
Next x

End Sub

通过一些Data Type转换来演示不同Type的返回:

Sub Test()

Dim arr1 As Variant: arr1 = Array(CDbl(4), CLng(5), CStr(3), CDate(7), CCur(6))
Dim arr2 As Variant: arr2 = Array(2)
Dim arr3 As Variant: arr3 = arr2

ReDim Preserve arr3(UBound(arr1) + Ubound(arr2) + 1)
For x = (UBound(arr3) - UBound(arr1)) To UBound(arr3)
    arr3(x) = arr1(x - UBound(arr2) - 1)
Next x

End Sub

enter image description here