Check if empty string


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
'**********************************************************************************************************
' Name:                 isEmptyString
' Author:               mielk | 2014-09-20
'
' Description:          Function to check if the given value is empty string.
'                       Note that strings consisting only of blank characters (i.e. spaces) are also
'                       considered to be an empty string.
'
' Parameters:
'   value               Value to be checked.
'
'
' Returns:
'   Boolean             True - if the given value is empty or cannot be converted to String type.
'                           True value is returned in a few cases:
'                           * the source parameter [value] is an empty string,
'                           * the source parameter [value] is a string consisting only of blank characters,
'                           * the source parameter [value] is not a string and cannot be converted into
'                             string (i.e it is object or array).
'                       False - if the given value is a non-empty string or other primitive value that can
'                               be converted into String.
'
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2014-09-20        mielk       Function created.
'**********************************************************************************************************
Public Function isEmptyString(ByVal value As Variant) As Boolean
    Const METHOD_NAME As String = "isEmptyString"
    '------------------------------------------------------------------------------------------------------


    'First check if the given value can be converted into String. If not, function should return --------|
    'True, so the code moves to the [NotStringException] label, change the value of function to         '|
    'True and leaves the function.                                                                      '|
    On Error GoTo NotStringException                                                                    '|
    value = VBA.CStr(value)                                                                             '|
    On Error GoTo 0                                                                                     '|
    '----------------------------------------------------------------------------------------------------|


    'Check different cases when the function should return True. ----------------------------------------|
    If VBA.IsMissing(value) Then                                                                        '|
        isEmptyString = True                                                                            '|
    ElseIf VBA.isEmpty(value) Then                                                                      '|
        isEmptyString = True                                                                            '|
    ElseIf VBA.Len(VBA.Trim(value)) = 0 Then                                                            '|
        isEmptyString = True                                                                            '|
    End If                                                                                              '|
    '----------------------------------------------------------------------------------------------------|


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

'----------------------------------------------------------------------------------------------------------
NotStringException:
    isEmptyString = True
    GoTo ExitPoint

End Function