Is in array


Since function isInArray 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
'**********************************************************************************************************
' Name:                 isInArray
' Author:               mielk | 2011-07-19
'
' Comment:              Checks if a specified value is contained in the given array.
'
' Parameters:
'   value               String to be checked.
'   arr                 Array to be tested.
'                       Each item of this array is compared to source value [value] and if any of them is
'                       equal to this string, function returns True.
'                       Parameter [arr] has to be a one-dimensional array that contains only values of
'                       primitive types. If it contain any object, exception ObjectsInArrayException will
'                       be thrown.
'   isCaseSensitive     Optional parameter of Boolean type.
'                       It determines if text comparing is case sensitive.
'                       If this value is set to True, comparing is case sensitive - a letter in lowercase
'                       is treated as different than the same letter in uppercase (i.e. a ? A).
'                       If this value is set to False, it doesn't matter if a letter is in lowercase or in
'                       uppercase, since both of them are considered as the same character (i.e. a = A).
'                       Default value of this parameter is False.
'
' Returns:
'   Boolean             True - if any item of the given array [arr] is equal to the
'                               specified string [str].
'                       False - otherwise.
'
' Exceptions:
'   NotArrayException               Thrown when the given [arr] parameter is not an array.
'   ObjectsInArrayException         Thrown when the given array contains not only values of primitive
'                                   types, but also objects.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2011-07-19    mielk       Class created.
'**********************************************************************************************************
Public Function isInArray(value As String, arr As Variant, _
                                                    Optional isCaseSensitive As Boolean = False) As Boolean
    Const METHOD_NAME As String = "startsWith"
    Dim SEPARATOR As String: SEPARATOR = VBA.Chr(0)
    '------------------------------------------------------------------------------------------------------
    Dim tempStr As String
    Dim uCompareMethod As VBA.VbCompareMethod
    '------------------------------------------------------------------------------------------------------


    'Checks if the given parameter [arr] is an array. If not, code moves to the -------------------------|
    'NotArrayException label.                                                                           '|
    If countDimensions(arr) <> 1 Then GoTo NotArrayException                                            '|
    '----------------------------------------------------------------------------------------------------|


    'Convert [isCaseSensitive] parameter of Boolean type to the [VbCompareMethod] enumeration. ----------|
    If isCaseSensitive Then                                                                             '|
        uCompareMethod = VBA.vbBinaryCompare                                                            '|
    Else                                                                                                '|
        uCompareMethod = VBA.vbTextCompare                                                              '|
    End If                                                                                              '|
    '----------------------------------------------------------------------------------------------------|


    'Try to convert the given array into String using VBA-built-in function Join. If there is -----------|
    'any object in the given array error will be generated and macro moves to the                       '|
    'ObjectsInArrayException label.                                                                     '|
    On Error GoTo ObjectsInArrayException                                                               '|
    tempStr = SEPARATOR & VBA.Join(arr, SEPARATOR) & SEPARATOR                                          '|
    On Error GoTo 0                                                                                     '|
    '----------------------------------------------------------------------------------------------------|


    isInArray = VBA.InStr(1, tempStr, SEPARATOR & value & SEPARATOR, uCompareMethod)


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


'-----------------------------------------------
NotArrayException:
    '(...)
    'Put your own error handling here for a case if the given parameter [arr] is not array.

    GoTo ExitPoint


ObjectsInArrayException:
    '(...)
    'Put your own error handling here for a case if the given array contains any object.

    GoTo ExitPoint


End Function