This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
modValidation.bas
76 lines (60 loc) · 3.14 KB
/
modValidation.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
71
72
73
74
75
76
Attribute VB_Name = "modValidation"
Option Explicit
'// these are the column constraints on the database
Public Const MAXPRODUCTNAMELEN As Integer = 50
Public Const MAXPACKAGELEN As Integer = 30
'// unit price check
Public Const MINUNITPRICE As Double = 0
Function SheetDataValidation(Target As Range) As Boolean
Dim rngIntersect As Range
Dim intMaxChars As Integer
intMaxChars = shtControl.Range("rMaxProductNameLength")
'// default the function to true
SheetDataValidation = True
Set rngIntersect = shtData.Range("rProductName")
Set rngIntersect = rngIntersect.Resize(rngIntersect.CurrentRegion.Rows.Count - 1, rngIntersect.Columns.Count)
Set rngIntersect = rngIntersect.Offset(1, 0) '// offset to exclude headers
'// The variable rngIntersect contains the cells that will
'// cause an alert when they are changed.
If Not Application.Intersect(rngIntersect, Range(Target.Address)) Is Nothing Then
If Len(Target.Text) > shtControl.Range("rMaxProductNameLength") Then
Target.Interior.ColorIndex = 3
MsgBox "Maximum validation length set on Control sheet for Product Name is: " & intMaxChars & _
vbNewLine & vbNewLine & "Please enter text length less than " & intMaxChars & " characters.", vbInformation, "Validation Rule"
Target.Activate
SheetDataValidation = False
Exit Function
End If
'// default the cell colour
Target.Interior.ColorIndex = -4142
End If
Set rngIntersect = shtData.Range("rUnitPrice")
Set rngIntersect = rngIntersect.Resize(rngIntersect.CurrentRegion.Rows.Count - 1, rngIntersect.Columns.Count)
Set rngIntersect = rngIntersect.Offset(1, 0) '// offset to exclude headers
If Not Application.Intersect(rngIntersect, Range(Target.Address)) Is Nothing Then
If Not IsNumeric(Target.Value) Or Target.Value < MINUNITPRICE Then
Target.Interior.ColorIndex = 3
MsgBox "Unit price must be of a numerical value greater than 0", vbInformation, "Validation Rule"
Target.Activate
SheetDataValidation = False
Exit Function
End If
'// default the cell colour
Target.Interior.ColorIndex = -4142
End If
Set rngIntersect = shtData.Range("rPackage")
Set rngIntersect = rngIntersect.Resize(rngIntersect.CurrentRegion.Rows.Count - 1, rngIntersect.Columns.Count)
Set rngIntersect = rngIntersect.Offset(1, 0) '// offset to exclude headers
If Not Application.Intersect(rngIntersect, Range(Target.Address)) Is Nothing Then
If Len(Target.Text) > MAXPACKAGELEN Then
Target.Interior.ColorIndex = 3
MsgBox "Maximum validation length set on by the database is: " & MAXPACKAGELEN & _
vbNewLine & vbNewLine & "Please enter text length less than " & MAXPACKAGELEN & " characters.", vbInformation, "Validation Rule"
Target.Activate
SheetDataValidation = False
Exit Function
End If
'// default the cell colour
Target.Interior.ColorIndex = -4142
End If
End Function