Join dictionaries


Since function joinDictionaries uses some non-built VBA functions, they also must be included in your code for the function to work properly.

Otherwise the following error will occur: Compile error: Sub or Function not defined.

Required functions are listed below. You can get to each function's source code by clicking its name:

When adding the functions above to your VBA project, make sure you haven't done it before. If there are two different public functions with the same name in a single VBA project, the following compilation error is thrown: Compile error: Ambiguous name detected: function_name.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'**********************************************************************************************************
' Name:                 joinDictionaries
' Author:               mielk | 2014-07-15
'
' Comment:              Function to join given dictionaries into a single Dictionary.
'                       * Function can join up to 30 dictionaries.
'
' Parameters:
'   ignoreErrors        Parameter of Boolean type. It defines if function should stop working if any error
'                       occurred and raise an error or if it should just skip the input parameter that
'                       caused this error and go to the next one.
'   dictionaries        Dictionaries to be joined.
'                       This parameter is an array of ParamArray type. It means you can add as many
'                       dictionaries as you want (up to 30).
'
'
' Returns:
'   Dictionary          Object of Scripting.Dictionary type containing all the items from the
'                       dictionaries given to the function in dictionaries array.
'
'
' Exceptions:
'   IllegalTypeException        Thrown if the parameter passed to this function is not a dictionary.
'   KeyAlreadyExistsException   Thrown if a key exists in more than one source dictionary.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2014-07-15    mielk       Method created.
'**********************************************************************************************************
Public Function joinDictionaries(ignoreErrors As Boolean, ParamArray dictionaries() As Variant) As Object
    Const METHOD_NAME As String = "joinDictionaries"
    '------------------------------------------------------------------------------------------------------
    Dim dictResults As Object
    Dim varDictionary As Variant
    Dim varKey As Variant
    '------------------------------------------------------------------------------------------------------


    'Create a new, empty Dictionary for storing results. ------------------------------------------------|
    Set dictResults = VBA.CreateObject("Scripting.Dictionary")                                          '|
    '----------------------------------------------------------------------------------------------------|


    'Iterate through all the items of dictionaries source array. ----------------------------------------|
    For Each varDictionary In dictionaries                                                              '|
                                                                                                        '|
        'Check if current item is of a dictionary type. ---------------------------------------------|  '|
        If compareString(VBA.TypeName(varDictionary), "dictionary") Then                            '|  '|
                                                                                                    '|  '|
            'If current item is of a dictionary type, it is being added to the ------------------|  '|  '|
            'result dictionary.                                                                 '|  '|  '|
            For Each varKey In varDictionary.keys                                               '|  '|  '|
                                                                                                '|  '|  '|
                'Before adding item to the dictionary, it must be checked if there is -------|  '|  '|  '|
                'already item with such key.                                                '|  '|  '|  '|
                'If there is no such item yet, it is being added to the result dictionary.  '|  '|  '|  '|
                'If there is already such item, function raise an error and finish working  '|  '|  '|  '|
                'or just skip this duplicated item (depending on the value of parameter     '|  '|  '|  '|
                '[ignoreErrors].                                                            '|  '|  '|  '|
                If dictResults.Exists(varKey) Then                                          '|  '|  '|  '|
                    If Not ignoreErrors Then GoTo KeyAlreadyExistsException                 '|  '|  '|  '|
                Else                                                                        '|  '|  '|  '|
                    Call dictResults.add(varKey, varDictionary.item(varKey))                '|  '|  '|  '|
                End If                                                                      '|  '|  '|  '|
                '----------------------------------------------------------------------------|  '|  '|  '|
                                                                                                '|  '|  '|
            Next varKey                                                                         '|  '|  '|
            '------------------------------------------------------------------------------------|  '|  '|
                                                                                                    '|  '|
        Else                                                                                        '|  '|
                                                                                                    '|  '|
            'If current item is not a dictionary, it is ignored or the function raises              '|  '|
            'IllegalTypeException, depending on the value of parameter ignoreErrors.                '|  '|
            If Not ignoreErrors Then GoTo IllegalTypeException                                      '|  '|
                                                                                                    '|  '|
        End If                                                                                      '|  '|
        '--------------------------------------------------------------------------------------------|  '|
                                                                                                        '|
    Next varDictionary                                                                                  '|
    '----------------------------------------------------------------------------------------------------|



    Set joinDictionaries = dictResults



'==========================================================================================================
ExitPoint:
    Exit Function


'----------------------------------------------------------------------------------------------------------
IllegalTypeException:
    'Error handling for the case if any of given parameter is not of Dictionary type.
    GoTo ExitPoint


KeyAlreadyExistsException:
    'Error handling for the case if items are duplicated in the input dictionaries.
    GoTo ExitPoint


End Function