Join collections


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'**********************************************************************************************************
' Name:                 joinCollections
' Author:               mielk | 2012-03-12
'
' Comment:              Joins up to 30 collections into one larger collection.
'
' Parameters:
'   collections()       Collections to be merged into larger collection.
'                       This parameter is of a ParamArray type, so it is possible to pass any number of
'                       collection to be joined (up to 30).
'                       All non-collection parameters are ignored.
'
' Returns:
'   Collection          Collection that contains all the items from the subcollections passed to this
'                       function.
'                       * Items in the result collection can be duplicated.
'                       * Items are added to the result collection without key, even if they are stored
'                         with key in their original collection.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2013-04-26    mielk       Method created.
'**********************************************************************************************************
Public Function joinCollections(ParamArray collections() As Variant) As VBA.Collection
    Const METHOD_NAME As String = "joinCollections"
    '------------------------------------------------------------------------------------------------------
    Dim subcollection As Variant
    Dim item As Variant
    '------------------------------------------------------------------------------------------------------

    'Initialize result variable with empty Collection.
    Set joinCollections = New VBA.Collection


    'Iterate through all the given subcollections. --------------------------------------|
    For Each subcollection In collections                                               '|
                                                                                        '|
        'Check if an item currently processed in this loop is a collection.             '|
        If TypeOf subcollection Is Collection Then                                      '|
                                                                                        '|
            'If [subcollection] is a collection, iterate through all its ------------|  '|
            'items and add them to the result Collection [joinCollections].         '|  '|
            For Each item In subcollection                                          '|  '|
                Call joinCollections.add(item)                                      '|  '|
            Next item                                                               '|  '|
            '------------------------------------------------------------------------|  '|
                                                                                        '|
        End If                                                                          '|
                                                                                        '|
    Next subcollection                                                                  '|
    '------------------------------------------------------------------------------------|


End Function