-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationModule.bas
More file actions
523 lines (427 loc) · 17.1 KB
/
Copy pathValidationModule.bas
File metadata and controls
523 lines (427 loc) · 17.1 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
Attribute VB_Name = "ValidationModule"
' Manufacturing Workflow Scheduler - Validation Module
' Created: April 2025
'
' This module contains validation and testing functions to ensure data integrity
' and correct workflow definitions, following software quality best practices.
Option Explicit
' Validate imported data
Public Function ValidateImportedData() As Boolean
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim errors As Collection
Dim result As Boolean
On Error GoTo ErrorHandler
Set errors = New Collection
result = True
' Set reference to data sheet
Set ws = ThisWorkbook.Sheets("ImportedData")
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' Check if data exists
If lastRow <= 1 Then
errors.Add "No data found in the ImportedData sheet."
result = False
GoTo Finalize
End If
' Validate required columns
If ws.Range("A1").Value <> "WorkOrder" Then errors.Add "Column A should be 'WorkOrder'"
If ws.Range("B1").Value <> "Product" Then errors.Add "Column B should be 'Product'"
If ws.Range("C1").Value <> "Quantity" Then errors.Add "Column C should be 'Quantity'"
If ws.Range("D1").Value <> "StartDate" Then errors.Add "Column D should be 'StartDate'"
If ws.Range("E1").Value <> "DueDate" Then errors.Add "Column E should be 'DueDate'"
If ws.Range("F1").Value <> "Priority" Then errors.Add "Column F should be 'Priority'"
If errors.Count > 0 Then
result = False
GoTo Finalize
End If
' Validate data rows
For i = 2 To lastRow
' Check for empty cells in required fields
If Trim(ws.Cells(i, 1).Value) = "" Then
errors.Add "Row " & i & ": WorkOrder is required"
result = False
End If
If Trim(ws.Cells(i, 2).Value) = "" Then
errors.Add "Row " & i & ": Product is required"
result = False
End If
If Trim(ws.Cells(i, 3).Value) = "" Or Not IsNumeric(ws.Cells(i, 3).Value) Then
errors.Add "Row " & i & ": Quantity must be a number"
result = False
End If
' Validate dates
If Not IsDate(ws.Cells(i, 4).Value) Then
errors.Add "Row " & i & ": StartDate must be a valid date"
result = False
End If
If Not IsDate(ws.Cells(i, 5).Value) Then
errors.Add "Row " & i & ": DueDate must be a valid date"
result = False
End If
' Check logical constraints
If IsDate(ws.Cells(i, 4).Value) And IsDate(ws.Cells(i, 5).Value) Then
If CDate(ws.Cells(i, 4).Value) > CDate(ws.Cells(i, 5).Value) Then
errors.Add "Row " & i & ": StartDate cannot be after DueDate"
result = False
End If
End If
Next i
Finalize:
' Display errors if any
If Not result Then
Dim errorMsg As String
errorMsg = "Data validation failed with the following errors:" & vbCrLf & vbCrLf
For i = 1 To errors.Count
errorMsg = errorMsg & "• " & errors(i) & vbCrLf
Next i
MsgBox errorMsg, vbExclamation, "Validation Errors"
End If
ValidateImportedData = result
Exit Function
ErrorHandler:
MsgBox "Error validating data: " & Err.Description, vbExclamation
ValidateImportedData = False
End Function
' Validate workflow definition
Public Function ValidateWorkflowDefinition() As Boolean
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long, j As Long
Dim errors As Collection
Dim result As Boolean
Dim stepIds As Collection
On Error GoTo ErrorHandler
Set errors = New Collection
Set stepIds = New Collection
result = True
' Set reference to workflow sheet
Set ws = ThisWorkbook.Sheets("WorkflowSetup")
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
' Check if data exists
If lastRow <= 1 Then
errors.Add "No workflow steps found in the WorkflowSetup sheet."
result = False
GoTo Finalize
End If
' Validate required columns
If ws.Range("A1").Value <> "StepID" Then errors.Add "Column A should be 'StepID'"
If ws.Range("B1").Value <> "StepName" Then errors.Add "Column B should be 'StepName'"
If ws.Range("C1").Value <> "Department" Then errors.Add "Column C should be 'Department'"
If ws.Range("D1").Value <> "Duration" Then errors.Add "Column D should be 'Duration'"
If ws.Range("E1").Value <> "Prerequisites" Then errors.Add "Column E should be 'Prerequisites'"
If ws.Range("F1").Value <> "ResourceRequired" Then errors.Add "Column F should be 'ResourceRequired'"
If ws.Range("G1").Value <> "SetupTime" Then errors.Add "Column G should be 'SetupTime'"
If errors.Count > 0 Then
result = False
GoTo Finalize
End If
' Collect all step IDs
On Error Resume Next
For i = 2 To lastRow
If Trim(ws.Cells(i, 1).Value) <> "" Then
stepIds.Add ws.Cells(i, 1).Value, CStr(ws.Cells(i, 1).Value)
End If
Next i
On Error GoTo ErrorHandler
' Validate data rows
For i = 2 To lastRow
' Check for empty cells in required fields
If Trim(ws.Cells(i, 1).Value) = "" Then
errors.Add "Row " & i & ": StepID is required"
result = False
End If
If Trim(ws.Cells(i, 2).Value) = "" Then
errors.Add "Row " & i & ": StepName is required"
result = False
End If
If Trim(ws.Cells(i, 3).Value) = "" Then
errors.Add "Row " & i & ": Department is required"
result = False
End If
If Trim(ws.Cells(i, 4).Value) = "" Or Not IsNumeric(ws.Cells(i, 4).Value) Then
errors.Add "Row " & i & ": Duration must be a number"
result = False
ElseIf CDbl(ws.Cells(i, 4).Value) <= 0 Then
errors.Add "Row " & i & ": Duration must be greater than zero"
result = False
End If
If Trim(ws.Cells(i, 6).Value) = "" Then
errors.Add "Row " & i & ": ResourceRequired is required"
result = False
End If
If Trim(ws.Cells(i, 7).Value) = "" Or Not IsNumeric(ws.Cells(i, 7).Value) Then
errors.Add "Row " & i & ": SetupTime must be a number"
result = False
ElseIf CDbl(ws.Cells(i, 7).Value) < 0 Then
errors.Add "Row " & i & ": SetupTime cannot be negative"
result = False
End If
' Validate prerequisites
If Trim(ws.Cells(i, 5).Value) <> "" Then
Dim prereqs As Variant
prereqs = Split(ws.Cells(i, 5).Value, ",")
For j = 0 To UBound(prereqs)
Dim prereq As String
prereq = Trim(prereqs(j))
' Check if prerequisite exists
Dim prereqExists As Boolean
prereqExists = False
On Error Resume Next
If stepIds(prereq) <> "" Then prereqExists = True
On Error GoTo ErrorHandler
If Not prereqExists Then
errors.Add "Row " & i & ": Prerequisite '" & prereq & "' is not defined in the workflow"
result = False
End If
Next j
End If
Next i
' Check for circular dependencies
If Not CheckCircularDependencies(ws, stepIds, errors) Then
result = False
End If
Finalize:
' Display errors if any
If Not result Then
Dim errorMsg As String
errorMsg = "Workflow validation failed with the following errors:" & vbCrLf & vbCrLf
For i = 1 To errors.Count
errorMsg = errorMsg & "• " & errors(i) & vbCrLf
Next i
MsgBox errorMsg, vbExclamation, "Validation Errors"
End If
ValidateWorkflowDefinition = result
Exit Function
ErrorHandler:
MsgBox "Error validating workflow: " & Err.Description, vbExclamation
ValidateWorkflowDefinition = False
End Function
' Check for circular dependencies in workflow
Private Function CheckCircularDependencies(ws As Worksheet, stepIds As Collection, errors As Collection) As Boolean
Dim lastRow As Long
Dim i As Long
Dim dependencyMap As Object
Dim result As Boolean
result = True
Set dependencyMap = CreateObject("Scripting.Dictionary")
' Build dependency map
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
Dim stepId As String
Dim prerequisites As String
stepId = ws.Cells(i, 1).Value
prerequisites = ws.Cells(i, 5).Value
If Trim(prerequisites) <> "" Then
dependencyMap.Add stepId, prerequisites
Else
dependencyMap.Add stepId, ""
End If
Next i
' Check each step for circular dependencies
Dim key As Variant
For Each key In dependencyMap.Keys
Dim visited As Object
Set visited = CreateObject("Scripting.Dictionary")
If HasCircularDependency(key, dependencyMap, visited) Then
errors.Add "Circular dependency detected involving step '" & key & "'"
result = False
End If
Next key
CheckCircularDependencies = result
End Function
' Check if a step has circular dependencies
Private Function HasCircularDependency(stepId As String, dependencyMap As Object, visited As Object) As Boolean
' Mark as visited
visited.Add stepId, True
' Check prerequisites
If Trim(dependencyMap(stepId)) <> "" Then
Dim prereqs As Variant
prereqs = Split(dependencyMap(stepId), ",")
Dim i As Long
For i = 0 To UBound(prereqs)
Dim prereq As String
prereq = Trim(prereqs(i))
' Check if already visited
If visited.Exists(prereq) Then
HasCircularDependency = True
Exit Function
End If
' Check recursively
If dependencyMap.Exists(prereq) Then
Dim newVisited As Object
Set newVisited = CloneVisited(visited)
If HasCircularDependency(prereq, dependencyMap, newVisited) Then
HasCircularDependency = True
Exit Function
End If
End If
Next i
End If
HasCircularDependency = False
End Function
' Clone visited dictionary
Private Function CloneVisited(visited As Object) As Object
Dim newVisited As Object
Set newVisited = CreateObject("Scripting.Dictionary")
Dim key As Variant
For Each key In visited.Keys
newVisited.Add key, visited(key)
Next key
Set CloneVisited = newVisited
End Function
' Run validation tests
Public Sub RunAllValidations()
Dim dataValid As Boolean
Dim workflowValid As Boolean
' Validate imported data
dataValid = ValidateImportedData()
' Validate workflow
workflowValid = ValidateWorkflowDefinition()
' Show result
If dataValid And workflowValid Then
MsgBox "All validations passed successfully.", vbInformation, "Validation Complete"
Else
MsgBox "Validation failed. Please fix the errors before continuing.", vbExclamation, "Validation Failed"
End If
End Sub
' Test data generation for quick testing
Public Sub GenerateTestData()
Dim wsData As Worksheet
Dim wsWorkflow As Worksheet
' Create test data sheet if needed
If Not SheetExists("ImportedData") Then
ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = "ImportedData"
End If
Set wsData = ThisWorkbook.Sheets("ImportedData")
' Clear existing data
wsData.UsedRange.ClearContents
' Create test workflow sheet if needed
If Not SheetExists("WorkflowSetup") Then
ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = "WorkflowSetup"
End If
Set wsWorkflow = ThisWorkbook.Sheets("WorkflowSetup")
' Clear existing data
wsWorkflow.UsedRange.ClearContents
' Add test data headers
wsData.Range("A1").Value = "WorkOrder"
wsData.Range("B1").Value = "Product"
wsData.Range("C1").Value = "Quantity"
wsData.Range("D1").Value = "StartDate"
wsData.Range("E1").Value = "DueDate"
wsData.Range("F1").Value = "Priority"
' Add test workflow headers
wsWorkflow.Range("A1").Value = "StepID"
wsWorkflow.Range("B1").Value = "StepName"
wsWorkflow.Range("C1").Value = "Department"
wsWorkflow.Range("D1").Value = "Duration"
wsWorkflow.Range("E1").Value = "Prerequisites"
wsWorkflow.Range("F1").Value = "ResourceRequired"
wsWorkflow.Range("G1").Value = "SetupTime"
' Add test data rows
wsData.Range("A2").Value = "WO-001"
wsData.Range("B2").Value = "Widget A"
wsData.Range("C2").Value = 100
wsData.Range("D2").Value = Date
wsData.Range("E2").Value = DateAdd("d", 15, Date)
wsData.Range("F2").Value = "High"
wsData.Range("A3").Value = "WO-002"
wsData.Range("B3").Value = "Widget B"
wsData.Range("C3").Value = 50
wsData.Range("D3").Value = DateAdd("d", 2, Date)
wsData.Range("E3").Value = DateAdd("d", 20, Date)
wsData.Range("F3").Value = "Medium"
wsData.Range("A4").Value = "WO-003"
wsData.Range("B4").Value = "Assembly C"
wsData.Range("C4").Value = 75
wsData.Range("D4").Value = DateAdd("d", 5, Date)
wsData.Range("E4").Value = DateAdd("d", 25, Date)
wsData.Range("F4").Value = "High"
' Add test workflow rows
wsWorkflow.Range("A2").Value = "S001"
wsWorkflow.Range("B2").Value = "Material Preparation"
wsWorkflow.Range("C2").Value = "Warehouse"
wsWorkflow.Range("D2").Value = 2
wsWorkflow.Range("E2").Value = ""
wsWorkflow.Range("F2").Value = "Material Handler"
wsWorkflow.Range("G2").Value = 0.5
wsWorkflow.Range("A3").Value = "S002"
wsWorkflow.Range("B3").Value = "Cutting"
wsWorkflow.Range("C3").Value = "Fabrication"
wsWorkflow.Range("D3").Value = 1
wsWorkflow.Range("E3").Value = "S001"
wsWorkflow.Range("F3").Value = "CNC Operator"
wsWorkflow.Range("G3").Value = 1
wsWorkflow.Range("A4").Value = "S003"
wsWorkflow.Range("B4").Value = "Milling"
wsWorkflow.Range("C4").Value = "Fabrication"
wsWorkflow.Range("D4").Value = 3
wsWorkflow.Range("E4").Value = "S002"
wsWorkflow.Range("F4").Value = "Machinist"
wsWorkflow.Range("G4").Value = 0.5
wsWorkflow.Range("A5").Value = "S004"
wsWorkflow.Range("B5").Value = "Drilling"
wsWorkflow.Range("C5").Value = "Fabrication"
wsWorkflow.Range("D5").Value = 2
wsWorkflow.Range("E5").Value = "S002"
wsWorkflow.Range("F5").Value = "Drill Operator"
wsWorkflow.Range("G5").Value = 0.5
wsWorkflow.Range("A6").Value = "S005"
wsWorkflow.Range("B6").Value = "Assembly A"
wsWorkflow.Range("C6").Value = "Assembly"
wsWorkflow.Range("D6").Value = 4
wsWorkflow.Range("E6").Value = "S003,S004"
wsWorkflow.Range("F6").Value = "Assembler"
wsWorkflow.Range("G6").Value = 1
wsWorkflow.Range("A7").Value = "S006"
wsWorkflow.Range("B7").Value = "Painting"
wsWorkflow.Range("C7").Value = "Finishing"
wsWorkflow.Range("D7").Value = 2
wsWorkflow.Range("E7").Value = "S005"
wsWorkflow.Range("F7").Value = "Painter"
wsWorkflow.Range("G7").Value = 0.5
wsWorkflow.Range("A8").Value = "S007"
wsWorkflow.Range("B8").Value = "Quality Inspection"
wsWorkflow.Range("C8").Value = "QA"
wsWorkflow.Range("D8").Value = 1
wsWorkflow.Range("E8").Value = "S006"
wsWorkflow.Range("F8").Value = "QA Inspector"
wsWorkflow.Range("G8").Value = 0.25
wsWorkflow.Range("A9").Value = "S008"
wsWorkflow.Range("B9").Value = "Packaging"
wsWorkflow.Range("C9").Value = "Shipping"
wsWorkflow.Range("D9").Value = 1
wsWorkflow.Range("E9").Value = "S007"
wsWorkflow.Range("F9").Value = "Packer"
wsWorkflow.Range("G9").Value = 0.5
' Format the data
FormatTestData wsData, wsWorkflow
MsgBox "Test data generated successfully.", vbInformation, "Test Data"
End Sub
' Format test data
Private Sub FormatTestData(wsData As Worksheet, wsWorkflow As Worksheet)
' Format data sheet
With wsData.Range("A1:F1")
.Font.Bold = True
.Interior.Color = RGB(200, 200, 200)
End With
With wsData.Range("D:E")
.NumberFormat = "yyyy-mm-dd"
End With
wsData.UsedRange.Columns.AutoFit
' Format workflow sheet
With wsWorkflow.Range("A1:G1")
.Font.Bold = True
.Interior.Color = RGB(200, 200, 200)
End With
wsWorkflow.Range("D:D,G:G").NumberFormat = "0.00"
wsWorkflow.UsedRange.Columns.AutoFit
End Sub
' Check if a sheet exists
Private Function SheetExists(sheetName As String) As Boolean
Dim ws As Worksheet
On Error Resume Next
Set ws = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0
SheetExists = Not ws Is Nothing
End Function