Compare strings start


Since function equalStart 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
'**********************************************************************************************************
' Name:                 equalStart
' Author:               mielk | 2015-02-18
'
' Comment:              Function to check if two given strings starts with the same substring.
'
' Parameters:
'   baseString          Base string.
'   comparedString      String to be compared with base string.
'   chars               The number of characters to be checked.
'                       * It must be positive value or 0 (however, for 0 function always returns True).
'   isCaseSensitive     Optional parameter of Boolean type.
'                       It determines if text searching is case sensitive.
'                       * If this value is set to True, searching 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 True.
'
' Returns:
'   Boolean             True - if first [x] characters of the given base string is equal to the first [x]
'                              characters in the compared string, where [x] is defined by parameter
'                              [chars].
'                       False - otherwise.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2015-02-18        mielk           Class created.
'**********************************************************************************************************
Public Function equalStart(baseString As String, comparedString As String, chars As Integer, _
                                                    Optional isCaseSensitive As Boolean = False) As Boolean
    Const METHOD_NAME As String = "equalStart"
    '------------------------------------------------------------------------------------------------------
    Dim basePart As String
    Dim comparedPart As String
    '------------------------------------------------------------------------------------------------------


    'Check if the parameter [chars] is not negative. ----------------------------------------------------|
    If chars < 0 Then GoTo IllegalValueException                                                        '|
    '----------------------------------------------------------------------------------------------------|


    'Extract the proper part of the base and compared string. -------------------------------------------|
    basePart = VBA.Left$(baseString, chars)                                                             '|
    comparedPart = VBA.Left$(comparedString, chars)                                                     '|
    '----------------------------------------------------------------------------------------------------|


    'Check if both substrings are equal by using function [compareString].
    equalStart = compareString(basePart, comparedPart, isCaseSensitive)


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

'----------------------------------------------------------------------------------------------------------
IllegalValueException:
    'Error handling for the case if the given parameter [chars] is negative value.
    GoTo ExitPoint

End Function