Check if is in range


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
'**********************************************************************************************************
' Name:                 inRange
' Author:               mielk | 2014-02-10
'
' Comment:              Function to check if the given value is between two limiters defined as input
'                       parameters.
'                       Function can be used to check not only numbers but also other primitive values
'                       (strings, dates).
'
'
' Parameters:
'   baseValue           Value to be checked.
'                       * It can be of any primitive data type.
'                       * For objects and arrays function throws an error.
'   low                 Value used as a low limiter in comparison.
'                       Base value must be higher or equal to this value for function to return True.
'   up                  Value used as an up limiter in comparison.
'                       Base value must be lower or equal to this value for function to return True.
'
'
' Returns:
'   Boolean             True - if the given base value is between two limit values defined as [low] and
'                              [up] parameters.
'                       * [NOTE] Please be aware that for string comparison letter casing is important, ie.
'                           for limits [low] = "a" and [up] = "b" function will return True if
'                           [baseValue] = "ab" but False for the same value in upper case ("AB").
'                       False - otherwise.
'
'
'
' Exceptions:
'   IllegalTypeException            Thrown if any of the given parameters is an object or an array.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2013-04-10        mielk           Function created.
'**********************************************************************************************************
Public Function inRange(baseValue As Variant, low As Variant, up As Variant) As Boolean
    Const METHOD_NAME As String = "inRange"
    '------------------------------------------------------------------------------------------------------


    'Comparison is not possible if any of the parameters is array or object. In such case function ------|
    'moves to IllegalTypeException label.                                                               '|
    On Error GoTo IllegalTypeException                                                                  '|
    If baseValue >= low Then                                                                            '|
        If baseValue <= up Then                                                                         '|
            inRange = True                                                                              '|
        End If                                                                                          '|
    End If                                                                                              '|
    '----------------------------------------------------------------------------------------------------|



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

'----------------------------------------------------------------------------------------------------------
IllegalTypeException:
    'Error handling for the case if any of the given parameters is an object or an array.
    GoTo ExitPoint

End Function