我目前正在尝试将 46 个数组合并为一个数组。我已经在互联网上搜索过,但没有成功,希望这里有人能提供帮助。我确实找到了下面的页面,但我需要能够在嵌套的 for 循环中查看新数组的每个元素,因此使用下面的方法并不能完全实现我的最终目标。
基本上,我需要以这样的方式组合我的 46 个数组,然后我可以使用嵌套的 for 循环遍历每个元素。IE。
数组集:
myArray1 = (1, 2, 3, 4)
myArray2 = (5, 6, 7)
myArray3 = (8, 9)
myArray4 = (10, 11, 12, 13, 14)
.
.
.
myArray46 = (101, 102, 103)
Run Code Online (Sandbox Code Playgroud)
将它们组合起来形成新的数组:
myNewArray = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14... 101, 102, 103)
Run Code Online (Sandbox Code Playgroud)
在嵌套的 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
Run Code Online (Sandbox Code Playgroud)
非常感谢任何帮助和/或指导!
由于您在评论中写道,您的最终目标是创建一组唯一元素,因此最好使用字典,您可以在将每个元素添加到字典时测试唯一性。就像是:
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
Run Code Online (Sandbox Code Playgroud)
如果你运行Tester,你会看到mainArray包含:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
25
100
Run Code Online (Sandbox Code Playgroud)