Вот еще один пример:
Private Function CombineArray(arr1() As Variant, arr2() As Variant) As Variant()
Dim tmp() As Variant ' Array to hold values until output
Dim n As Long ' Numeric value to count first array
Dim m As Long ' Numeric value to count second array (`n` could be reused, but this helps keep things separate, IMHO)
Dim idx As Long ' Variable to keep tabs on current index when switching between arrays
' ReDim out temporary array
ReDim tmp(UBound(arr1) + UBound(arr2) + 1)
' Set an indexer to account for switching arrays
idx = 0
For n = 0 To UBound(arr1, 1)
tmp(idx) = arr1(n)
idx = idx + 1
Next n
' Iterate over second array.
For m = 0 To UBound(arr2, 1)
tmp(idx) = arr2(m)
idx = idx + 1
Next m
' Set results to function output
CombineArray = tmp
End Function
Затем функция быстрой проверки для проверки результатов CombineArray ():
Public Function TestFunc()
Dim output() As Variant
Dim emails() As Variant
Dim moreemails() As Variant
' Set out test email groups
emails() = Array("a@a.com", "b@b.com")
moreemails() = Array("c@c.com", "d@d.com")
' ReDim output array. Probably not necessary, but helps keep things in check.
ReDim output(UBound(emails) + UBound(moreemails))
' Populate output array with combined array
output = CombineArray(emails, moreemails)
' Test output
Debug.Print Chr(13) & "List of values:"
Dim i As Integer
For i = 0 To UBound(output)
Debug.Print "-- " & output(i)
Next i
' Join function output
Debug.Print Chr(13) & "The joined values are: " & Join(output, ";")
End Function
Используя непосредственное окно, мы получаем:
Debug.Print TestFunc()
List of values:
-- a@a.com
-- b@b.com
-- c@c.com
-- d@d.com
The joined values are: a@a.com;b@b.com;c@c.com;d@d.com