我目前正在尝试将46个阵列组合成一个阵列。我搜寻了互联网,但没有任何结果,希望这里有人能提供帮助。我确实找到了下面的页面,但是我需要能够在嵌套的for循环中浏览新数组的每个元素,因此使用下面的方法并不能完全达到我的最终目标。

Excel vba - combine multiple arrays into one

基本上,我需要以这样一种方式组合我的46个数组,以便可以使用嵌套的for循环遍历每个元素。即。

数组集:

myArray1 = (1, 2, 3, 4)
myArray2 = (5, 6, 7)
myArray3 = (8, 9)
myArray4 = (10, 11, 12, 13, 14)
.
.
.
myArray46 = (101, 102, 103)

合并它们以形成新的数组:
myNewArray = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14... 101, 102, 103)

遍历嵌套的for循环以对照我的主数组检查每个元素:
For i = LBound(mainArray) to UBound(mainArray)
    For j = LBound(myArray) to UBound(myArray)

    If mainArray(i) = myArray(j) Then
    'do something
    End If

    Next j
Next i

任何帮助和/或与此有关的指导,不胜感激!

最佳答案

由于您在评论中写道,最终目的是创建一组唯一的元素,因此最好使用字典,在将每个元素添加到字典时可以测试其唯一性。就像是:

Option Explicit
Function uniqueArr(ParamArray myArr() As Variant) As Variant()
    Dim dict As Object
    Dim V As Variant, W As Variant
    Dim I As Long

Set dict = CreateObject("Scripting.Dictionary")
For Each V In myArr 'loop through each myArr
    For Each W In V 'loop through the contents of each myArr
        If Not dict.exists(W) Then dict.Add W, W
    Next W
Next V


uniqueArr = dict.keys

End Function

Sub tester()
    Dim myArray1, myArray2, myArray3, myArray4, myArray5
    myArray1 = Array(1, 2, 3, 4)
    myArray2 = Array(5, 6, 7, 8)
    myArray3 = Array(9, 10, 11, 12, 13, 14)
    myArray4 = Array(15, 16)
    myArray5 = Array(1, 3, 25, 100)

Dim mainArray

mainArray = uniqueArr(myArray1, myArray2, myArray3, myArray4, myArray5)

End Sub

如果运行Tester,您将看到mainArray包含:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
25
100

10-08 01:42