Create array of strings


Since function createStringArray 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
'**********************************************************************************************************
' Name:                 createStringArray
' Author:               mielk | 2015-01-25
'
' Description:          Function to create 1D array with the given string items.
'
' Parameters:
'   items               Items to be inserted into result array.
'                       * This is parameter of ParamArray type. That means, function accept custom number
'                         of items (up to 30).
'                       * Each given item should be string.
'                       * Any primitive value other than string is converted to string before being
'                         inserted.
'                       * Any value that cannot be converted to String (i.e. array or object) is ignored.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2015-01-25        mielk           Function created.
'**********************************************************************************************************
Public Function createStringArray(ParamArray items() As Variant) As String()
    Const METHOD_NAME As String = "createStringArray"
    '------------------------------------------------------------------------------------------------------
    Dim result() As String
    Dim varItem As Variant
    '------------------------------------------------------------------------------------------------------

    'Iterate through all the elements in param array [items]. -------------------------------------------|
    For Each varItem In items                                                                           '|
                                                                                                        '|
        'Check if current item is not array nor object - those types of data cannot be converted ----|  '|
        'to string and they are ignored.                                                            '|  '|
        If Not VBA.IsArray(varItem) And Not VBA.IsObject(varItem) Then                              '|  '|
            Call addEntry(result, varItem)                                                          '|  '|
        End If                                                                                      '|  '|
        '--------------------------------------------------------------------------------------------|  '|
                                                                                                        '|
    Next varItem                                                                                        '|
    '----------------------------------------------------------------------------------------------------|


    'Assign result array to the result of this function.
    createStringArray = result


End Function