-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomValidation.bas
70 lines (62 loc) · 3.46 KB
/
CustomValidation.bas
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
Attribute VB_Name = "Module11"
Option Explicit
Public Function ValidateRegex(rngCell As Range, strPattern As String) As Boolean
'*******************************************************************************
'Purpose: Returns whether or not the value in the cell matches the regular
' expression pattern
'Inputs:
' - rngCell: Range containing a single cell whose value to check
' - strPattern: Regular expression to match
'Outputs: True for a match, False otherwise
'*******************************************************************************
Dim regEx As New RegExp
'Ensure input contains a single cell
If rngCell.Cells.Count > 1 Then
Err.Raise vbObjectError + 513, "Input range must be a single cell"
End If
regEx.Pattern = strPattern
ValidateRegex = regEx.Test(rngCell.Value)
End Function
Public Function ValidateColor(rngCell As Range) As Boolean
'*******************************************************************************
'Purpose: Returns whether or not the value in the cell is a valid "(A, R, G, B)"
' color
'Inputs:
' - rngCell: Range containing a single cell whose value to check
'Outputs: True for a valid color, False otherwise
'*******************************************************************************
Dim strColorComponentRegex As String: strColorComponentRegex = "(0|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])"
Dim strColorRegex As String: strColorRegex = "^\(" & strColorComponentRegex & ", " & strColorComponentRegex & ", " & strColorComponentRegex & ", " & strColorComponentRegex & "\)$"
ValidateColor = ValidateRegex(rngCell, strColorRegex)
End Function
Public Function ValidateMRN(rngCell As Range) As Boolean
'*******************************************************************************
'Purpose: Returns whether or not the value in the cell is a valid CRMC MRN
' (three zeroes followed by any six digits)
'Inputs:
' - rngCell: Range containing a single cell whose value to check
'Outputs: True for a valid MRN, False otherwise
'*******************************************************************************
ValidateMRN = ValidateRegex(rngCell, "^0{3}\d{6}$")
End Function
Public Function ValidateName(rngCell As Range) As Boolean
'*******************************************************************************
'Purpose: Returns whether or not the value in the cell is a valid name in the
' format: last comma first, possibly middle or middle initial (with or
' without a period
'Inputs:
' - rngCell: Range containing a single cell whose value to check
'Outputs: True for a valid name, False otherwise
'*******************************************************************************
ValidateName = ValidateRegex(rngCell, "^[A-Za-z-']+, [A-Za-z-']+([A-Za-z]\.?[A-Za-z-']*)?$")
End Function
Public Function ValidatePatient(rngCell As Range) As Boolean
'*******************************************************************************
'Purpose: Returns whether or not the value in the cell is a valid CRMC MRN or
' name
'Inputs:
' - rngCell: Range containing a single cell whose value to check
'Outputs: True for a valid MRN or name, False otherwise
'*******************************************************************************
ValidatePatient = ValidateMRN(rngCell) Or ValidateName(rngCell)
End Function