-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTableClass.cls
1083 lines (881 loc) · 37.6 KB
/
TableClass.cls
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "TableClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'@Folder("TableManager.Tables")
Option Explicit
Private Const Module_Name As String = "TableClass."
Private pTableName As String
Private pWkSht As Worksheet
Private pWorkbook As Workbook
Private pUserForms As Collection
Private pForm As FormClass
Private pCells As CellsClass
Private pTable As ListObject
Private pMaxHeader As Single
Private pMaxField As Single
Private pWrapCount As Single
Private pLocked As Boolean
Private pTarget As Range
Private Type TableType
' Headers
' The table's HeaderRowRange
' 2D array
' Only has row 1 (first parameter)
' The column names are in the column (second) parameter of the array
' If it exists, Row 0 is not used
' If it exists, Column 0 is not used
' Body
' The table's DataBodyRange
' Each table row is designated by the first (row) parameter of the array
' Each column is designated by the second (column) parameter of the array
' If it exists, Row 0 is not used
' If it exists, Column 0 is not used
' Valid
' "Valid" if TableType valid
' Error message if TableType invalid
Headers As Variant
Body As Variant
Valid As String
End Type
Private pDataTable As TableType
Private Type ColumnDesignatorType
ColumnName As String
ColumnNumber As Long
End Type
Private Sub Class_Initialize()
Set pCells = New CellsClass
End Sub ' Class_Initialize
Private Function ModuleList() As Variant
ModuleList = Array("TableRoutines.")
End Function ' ModuleList
Public Property Get ActiveRow() As Long
' Used in EventClass
ActiveRow = Me.DBRow
End Property
Public Property Get ActiveTarget() As Range
' Used in EventClass, TableClass
Set ActiveTarget = pTarget
End Property
Public Property Set ActiveTarget(ByVal Target As Range)
' Used in EventClass
Target.Select
Set pTarget = Target
End Property
Public Property Get DBRange() As Range
' Used in FormRoutines, TableClass, TableRoutines, CalendarBuilder
Set DBRange = pWkSht.ListObjects(pTableName).DataBodyRange
End Property
Public Property Get UserForms() As Workbook
Set UserForms = pUserForms
End Property
Public Property Set UserForms(ByVal vbl As Workbook)
Set pUserForms = vbl
End Property
Public Property Get DBColRange( _
ByVal Tbl As TableClass, _
ByVal ColumnName As String _
) As Range
' Used in TableRoutines, TableClass
Dim ColNum As Long
ColNum = Tbl.DBColNumber(ColumnName)
If ColNum = 0 Then
Err.Raise 1, "TableClass.DBColRange", "Fatal error. ColumnName not found."
End If
Set DBColRange = Tbl.Table.ListColumns(ColNum).DataBodyRange
End Property
Public Property Get DBColNumber(ByVal ColumnName As String) As Long
' Used in TableRoutines, TableClass
On Error Resume Next
DBColNumber = Application.WorksheetFunction.Match(ColumnName, Me.Table.HeaderRowRange, 0)
If Err.Number <> 0 Then DBColNumber = 0
On Error GoTo 0
End Property
Public Property Get DBRowNumber( _
ByVal ColumnName As String, _
ByVal Key As String _
) As Long
' Used in TableRoutines, TableClass
Dim KeyRange As Range
Set KeyRange = Me.DBColRange(Me, ColumnName)
On Error Resume Next
DBRowNumber = Application.WorksheetFunction.Match(Key, KeyRange, 0)
If Err.Number <> 0 Then DBRowNumber = 0
On Error GoTo 0
End Property
Public Property Get Form() As FormClass: Set Form = pForm: End Property
Public Property Set Form(ByVal Frm As FormClass): Set pForm = Frm: End Property
Public Property Get Locked() As Boolean: Locked = pLocked: End Property
Public Property Get MaxFieldLength() As Single: MaxFieldLength = pMaxField: End Property
Public Property Get MaxHeaderLength() As Single: MaxHeaderLength = pMaxHeader: End Property
Public Property Get Name() As String: Name = pTableName: End Property
Public Property Let Name(ByVal vbl As String): pTableName = vbl: End Property
Public Property Get NumColumns() As Long: NumColumns = pCells.Count: End Property
Public Property Get NumRows() As Long: NumRows = DBRange.Rows.Count: End Property
Public Property Get TableCells() As CellsClass: Set TableCells = pCells: End Property
Public Property Get Target() As Range: Set Target = pTarget: End Property
Public Property Set Target(ByVal vbl As Range): Set pTarget = vbl: End Property
Public Property Get Workbook() As Workbook: Set Workbook = pWorkbook: End Property
Public Property Get Worksheet() As Worksheet: Set Worksheet = pWkSht: End Property
Public Property Get WrapCount() As Long: WrapCount = pWrapCount: End Property
Public Property Get Table() As ListObject: Set Table = pTable: End Property
Public Property Set Table(ByVal Tbl As ListObject): Set pTable = Tbl: End Property
'@Ignore FunctionReturnValueNotUsed
Public Function CollectTableData( _
ByVal Wkbk As Workbook, _
ByVal Tbl As TableClass, _
ByVal ModuleName As String _
) As Boolean
' Used in TableClass
' Call CollectCellData to collect all the type information for each column of the table
Const RoutineName As String = Module_Name & "CollectTableData"
On Error GoTo ErrorHandler
Debug.Assert Initializing
Debug.Assert InScope(ModuleList, ModuleName)
pTableName = Tbl.Table.Name
Set pWkSht = Wkbk.Worksheets(Tbl.Table.Parent.Name)
Set pWorkbook = Wkbk
pLocked = True
Dim Element As Range
Dim OneCell As CellClass
For Each Element In pTable.HeaderRowRange
Set OneCell = New CellClass
OneCell.CollectCellData Tbl, Element, Module_Name
pCells.Add OneCell, Module_Name
If OneCell.ColumnWidth > pMaxField Then pMaxField = OneCell.ColumnWidth
If OneCell.Wrap Then pWrapCount = pWrapCount + 1
If OneCell.Length > pMaxHeader Then pMaxHeader = OneCell.Length
If Not OneCell.Locked Then pLocked = False
Next Element
If pLocked Then
MsgBox "All cells in the " & _
pTableName & _
" table are locked. No form created.", _
vbOKOnly Or vbCritical, _
"All Cells Locked"
End If
LoadTable Wkbk, Tbl.Table, pDataTable
CollectTableData = Not pLocked
'@Ignore LineLabelNotUsed
Done:
Exit Function
ErrorHandler:
RaiseError Err.Number, Err.Source, RoutineName, Err.Description
End Function ' CollectTableData
Public Function FirstCell() As Range
Const RoutineName As String = Module_Name & "FirstCell"
On Error GoTo ErrorHandler
' Used in EventClass, TableClass, TableRoutines
On Error Resume Next
Set FirstCell = pTable.DataBodyRange(1, 1)
On Error GoTo 0
If Err.Number = 91 Then
Set FirstCell = pTable.HeaderRowRange(2, 1)
End If
'@Ignore LineLabelNotUsed
Done:
Exit Function
ErrorHandler:
RaiseError Err.Number, Err.Source, RoutineName, Err.Description
End Function ' FirstCell
Public Function LastCell() As Range
' Used in TableClass, EventClass
Const RoutineName As String = Module_Name & "LastCell"
On Error GoTo ErrorHandler
Set LastCell = pTable.DataBodyRange(pTable.ListRows.Count, 1)
'@Ignore LineLabelNotUsed
Done:
Exit Function
ErrorHandler:
RaiseError Err.Number, Err.Source, RoutineName, Err.Description
End Function ' LastCell
Public Function LastRow() As Long
LastRow = pTarget.ListObject.ListRows.Count
End Function ' LastRow
Public Sub NextRow()
' Called in EventClass
Dim DBRow As Long
Dim I As Long
DBRow = Me.DBRow
If DBRow < pTarget.ListObject.ListRows.Count Then
TurnOnCellDescriptions Me, Module_Name
For I = DBRow To DBRange.Rows.Count
Set pTarget = pTarget.Offset(1)
If Not pTarget.Rows.Hidden Then Exit For
Next I
TurnOffCellDescriptions Me, Module_Name
Else
MsgBox "Cannot advance to the next row; already at last row", _
vbOKOnly Or vbExclamation, "Already at End of Table"
End If
End Sub ' NextRow
Public Sub PreviousRow()
' Called in EventClass
Dim DBRow As Long
Dim I As Long
DBRow = Me.DBRow
If DBRow > 1 Then
TurnOnCellDescriptions Me, Module_Name
For I = DBRow To DBRange.Rows.Count
Set pTarget = pTarget.Offset(-1)
If Not pTarget.Rows.Hidden Then Exit For
Next I
TurnOffCellDescriptions Me, Module_Name
Else
MsgBox "Cannot move to the previous row; already at first row", _
vbOKOnly Or vbExclamation, _
"Already at Beginning of Table"
End If
End Sub ' PreviousRow
Public Function DBRow() As Long
' Called in TableClass, FormRoutines, TableRoutines,
Dim THRow As Long: THRow = Me.ActiveTarget.ListObject.HeaderRowRange.Row
Dim TRow As Long: TRow = Me.ActiveTarget.Row
DBRow = TRow - THRow
End Function ' DBRow
Public Function SelectedDBCol(ByVal ColumnName As String) As Long
' Called in FormRoutines, TableRoutines
Dim Rng As Range
Dim vbl As Variant
Set Rng = Me.ActiveTarget.ListObject.HeaderRowRange
On Error Resume Next
vbl = Application.WorksheetFunction.Match(ColumnName, Rng, 0)
If Err.Number <> 0 Then
SelectedDBCol = 0
Exit Function
End If
On Error GoTo 0
SelectedDBCol = vbl
End Function ' SelectedDBCol
Public Function ColumnRange(ByVal ColNum As Long) As Range
' Called in TableRoutines
Set ColumnRange = pWkSht.Range(DBRange(1, ColNum), DBRange(NumRows, ColNum))
End Function
Public Function CellCount() As Long: CellCount = pCells.Count: End Function
' Called in FormClass, TableClass, FormRoutines, TableRoutines
Public Property Get Headers() As Variant
Headers = pDataTable.Headers
End Property
Public Property Get Body() As Variant
Body = pDataTable.Body
End Property
Public Property Let Body(ByVal vbl As Variant)
pDataTable.Body = vbl
End Property
Public Function GetData( _
Optional ByVal RowDesignator As Variant = "Empty", _
Optional ByVal ColumnDesignator As Variant = "Empty", _
Optional ByVal ColumnFilter As String = "Empty" _
) As Variant
' Purpose
' Return the subset of SearchTable as specified in the other parameters
' Assumptions
' Future:
' Add provisions for multiple filters; "And" only; no "Or"
' ColumnFilter becomes a parameter array
' Alternately, use this routine for the first filter then pass
' this routine's output to another routine for the next filter
' If RowDesignator is a number, that's the row to return
' If RowDesignator is "Empty" return the rows specified by ColumnFilter
' If ColumnDesignator has a value, that's the column to return
' If ColumnDesignator = "Empty", select the entire row
'
' If ColumnFilter contains "=", there's only one row to return
' If ColumnFilter contains "<>", "<', ">", "<=", or ">="
' there are (potentially) multiple rows to return
'
' Symbology for the table:
' RowDesignator (RD) can be numeric (N) or "Empty" (E)
' ColumnDesignator (CD) can be specified (S) or "Empty" (E)
' ColumnFilter (CF) can result in
' 0 hits (0)
' 1 hit (1)
' Multiple hits (M)
' "Empty" (E)
'
' Table below:
' RD CD CF Result
' NS0 Makes no sense
' NS1 Makes no sense
' NSM Makes no sense
' NSE Single value
' NE0 Makes no sense
' NE1 Makes no sense
' NEM Makes no sense
' NEE One row
' ES0 No data
' ES1 Single value
' ESM One column
' ESE One column
' EE0 No data
' EE1 Single value
' EEM Multiple rows, All columns
' EEE All Rows, All columns - the entire table
'
' The output can be
' No data
' ColumnFilter evaluates to 0 rows (regardless of RowDesignator and ColumDesignator value)
' A single value
' RowDesignator is numeric and ColumnDesignator is specified and ColumnFilter is empty
' A single row
' RowDesignator is numeric and ColumnDesignator is "Empty" and ColumnFilter is empty
' RowDesignator is "Empty" and ColumnDesignator is specified and ColumnFilter evaluates to a single row
' RowDesignator is "Empty" and ColumnDesignator is "Empty" and ColumnFilter evaluates to a single row
' A single column
' RowDesignator is "Empty" and ColumnDesignator is specified and ColumnFilter is "Empty"
' RowDesignator is "Empty" and ColumnDesignator is specified and ColumnFilter evaluates to multiple rows
' An array of rows and columns
' RowDesignator is "Empty" and ColumnDesignator is "Empty" and ColumnFilter evaluates to multiple rows
' All rows and columns
' RowDesignator is "Empty" and ColumnDesignator is "Empty" and ColumnFilter is "Empty"
' Makes no sense to specify a row and a filter
' RowDesignator is numeric and ColumnFilter <> "Empty"
'
' Error messages:
' "Error Table" if the SearchTable is invalid
' "Error Row Designator" if Rowdesignator is invalid
' "Error RowDesignator Out of Range"
' "Error Column Designator" if ColumnDesignator is invalid
' "Error Column Name Not Found"
' "Error Filter" if ColumnFilter is invalid
' "Error No Data" if ColumnFilter eliminates all the rows
' "Error Can't have a specific row and a column filter"
' Note that the calling routine need only check for "Error"
' to determine if there's an error and need only
' go deeper if necessary
' Start of code
'
Const Routine_Name As String = Module_Name & "GetData"
On Error GoTo ErrorHandler
' Verify that SearchTable is valid
If Left$(pDataTable.Valid, 5) = "Error" Then
pDataTable.Valid = "Error Table"
Exit Function
End If
' Verify that RowDesignator is valid
Dim RowNumber As Long
Dim TempRowDesignator As Variant
TempRowDesignator = ValidRowDesignator(pDataTable, RowDesignator)
Select Case Left$(TempRowDesignator, 5)
Case "Error" ' Invalid RowDesignator
GetData = "Error Row Designator"
Exit Function
Case "Empty" ' Empty RowDesignator
RowNumber = 0
Case Else ' Conclude that RowDesignator must be a number
If ColumnFilter <> "Empty" Then
GetData = "Error Can't have a specific row and a column filter"
Exit Function
End If
RowNumber = TempRowDesignator
End Select
' Verify that ColumnDesignator is valid
Dim ColumnNumber As Long
Dim TempColumnDesignator As ColumnDesignatorType
TempColumnDesignator = ValidColumnDesignator(pDataTable, ColumnDesignator)
Select Case Left$(TempColumnDesignator.ColumnName, 5)
Case "Error" ' Invalid ColumnDesignator
GetData = "Error Column Designator"
Exit Function
Case "Empty" ' Empty ColumnDesignator
ColumnNumber = 0
Case Else
ColumnNumber = TempColumnDesignator.ColumnNumber
End Select
' Verify that ColumnFilter is valid and set up the SearchTable
Dim ThisSearchTable As TableType
ThisSearchTable = ValidFilter(pDataTable, ColumnFilter)
Select Case Left$(ThisSearchTable.Valid, 5)
Case "Error"
GetData = ThisSearchTable.Valid
Exit Function
Case "Empty" ' An empty filter means return all the rows
ThisSearchTable = pDataTable
Case Else
' ThisSearchTable is already set up in the ValidFilter call
End Select
Dim RowCount As Long
RowCount = UBound(ThisSearchTable.Body, 1)
' SearchTable, RowDesignator, ColumnDesignator, and ColumnFilter are all valid
If RowDesignator <> "Empty" Then ' Valid RowDesignator
If ColumnDesignator <> "Empty" Then ' Valid ColumnDesignator
If ColumnFilter <> "Empty" Then ' Valid ColumnFilter
' The case where RowDesignator <> "Empty" and ColumnFilter <> "Empty" cannot exist
GetData = "Error Can't have a specific row and a column filter"
Else ' 1 row, 1 column, empty filter; one cell
GetData = ThisSearchTable.Body(RowNumber, ColumnNumber)
' pDataTable.Valid = "Valid"
End If ' ColumnFilter <> "Empty"
Else ' Empty ColumnDesignator
If ColumnFilter <> "Empty" Then
' The case where RowDesignator <> "Empty" and ColumnFilter <> "Empty" cannot exist
Else ' 1 row, unspecified column, empty filter; entire row
GetData = GetRow(ThisSearchTable, RowDesignator)
' pDataTable.Valid = "Valid"
End If ' ColumnFilter <> "Empty"
End If ' ColumnDesignator <> "Empty"
Else ' Empty RowDesignator
If ColumnDesignator <> "Empty" Then ' Valid ColumnDesignator
If ColumnFilter <> "Empty" Then ' Valid ColumnFilter
Select Case RowCount
Case 0 ' unspecified row, 1 column, filter=0 rows; no data
GetData = "Empty"
Case 1 ' unspecified row, 1 column, filter=1 row; one cell
GetData = GetColumn(ThisSearchTable, ColumnNumber)
' pDataTable.Valid = "Valid"
Case Else ' unspecified row, 1 column, filter=multiple rows; one column
GetData = GetColumn(ThisSearchTable, ColumnNumber)
' pDataTable.Valid = "Valid"
End Select
Else ' empty row, one column, empty filter; one entire column
GetData = GetColumn(ThisSearchTable, ColumnNumber)
' pDataTable.Valid = "Valid"
End If ' ColumnFilter <> "Empty"
Else ' Empty ColumnDesignator
If ColumnFilter <> "Empty" Then ' Valid ColumnFilter
Select Case RowCount
Case 0 ' unspecified row, unspecified column, filter=0 rows; no data
GetData = "Empty"
Case 1 ' unspecified row, unspecified column, filter=1 row; one row, all columns
GetData = ThisSearchTable.Body
Case Else ' unspecified row, unspecified column, filter=multiple rows; multiple rows, all columns
GetData = ThisSearchTable.Body
End Select
Else ' empty row, empty column, empty filter; entire table
GetData = ThisSearchTable.Body
End If ' ColumnFilter <> "Empty"
End If ' ColumnDesignator <> "Empty"
End If ' RowDesignator <> "Empty"
' pDataTable.Headers = ThisSearchTable.Headers
'@Ignore LineLabelNotUsed
Done:
Exit Function
ErrorHandler:
RaiseError Err.Number, Err.Source, Routine_Name, Err.Description
End Function
Private Property Get Valid() As Boolean
Valid = pDataTable.Valid
End Property
Private Property Let Valid(ByVal Val As Boolean)
pDataTable.Valid = Val
End Property
Private Function GetRow( _
SearchTable As TableType, _
ByVal RowNum As Long _
) As Variant
' Purpose
' Returns the row designated by RowNum
' Assumptions
Const Routine_Name As String = Module_Name & "GetRow"
On Error GoTo ErrorHandler
' Verify that SearchTable is valid
If Left$(SearchTable.Valid, 5) = "Error" Then
GetRow.Valid = "Error Table"
Exit Function
End If
Dim Ary As Variant
Dim NumberOfColumns As Long
Dim LastError As Long
On Error Resume Next ' UBound(SearchTable.Headers, 2) raises an error if there's only one column
NumberOfColumns = UBound(SearchTable.Headers, 2)
LastError = Err.Number
On Error GoTo ErrorHandler
If LastError = 0 Then
ReDim Ary(1, NumberOfColumns)
Dim I As Long
For I = 1 To UBound(SearchTable.Headers, 2)
Ary(1, I) = SearchTable.Body(RowNum, I)
Next I
GetRow = Ary
Else
GetRow = SearchTable.Body(RowNum, 1)
End If
'@Ignore LineLabelNotUsed
Done:
Exit Function
ErrorHandler:
RaiseError Err.Number, Err.Source, Routine_Name, Err.Description
End Function
Private Function GetColumn( _
SearchTable As TableType, _
ByVal ColumnNum As Long _
) As Variant
' Purpose
' Returns the column designated by ColumnNum
' Assumptions
Const Routine_Name As String = Module_Name & "GetColumn"
On Error GoTo ErrorHandler
' Verify that SearchTable is valid
If Left$(SearchTable.Valid, 5) = "Error" Then
GetColumn.Valid = "Error Table"
Exit Function
End If
Dim Ary As Variant
ReDim Ary(UBound(SearchTable.Body, 1), 1)
Dim I As Long
For I = 1 To UBound(SearchTable.Body, 1)
Ary(I, 1) = SearchTable.Body(I, ColumnNum)
Next I
GetColumn = Ary
'@Ignore LineLabelNotUsed
Done:
Exit Function
ErrorHandler:
RaiseError Err.Number, Err.Source, Routine_Name, Err.Description
End Function
Private Function ValidRowDesignator( _
SearchTable As TableType, _
ByVal RowDesignator As Variant _
) As Variant
' Purpose
' Determine if RowDesignator is valid
' Return the valid row number or "Empty" if valid
' Return ValidRowDesignator = "Error RowDesignator Out of Range" or
' "Error Row Designator" if invalid
' Assumptions
Const Routine_Name As String = Module_Name & "ValidRowDesignator"
On Error GoTo ErrorHandler
' Verify that SearchTable is valid
If Left$(SearchTable.Valid, 5) = "Error" Then
ValidRowDesignator.Valid = "Error Table"
Exit Function
End If
If RowDesignator = "Empty" Then
' Empty is a valid entry; means return an entire column
ValidRowDesignator = "Empty"
Else
Select Case VarType(RowDesignator)
Case vbInteger, vbLong
' RowDesignator is numeric
' Verify that RowDesignator is in the range of the table's rows
If RowDesignator >= 1 And RowDesignator <= UBound(SearchTable.Body, 1) Then
ValidRowDesignator = RowDesignator
Else
' RowDesignator is out of range
ValidRowDesignator = "Error RowDesignator Out of Range"
End If
Case Else
' Erroneous RowDesignator data type
ValidRowDesignator = "Error Row Designator"
End Select
End If
'@Ignore LineLabelNotUsed
Done:
Exit Function
ErrorHandler:
RaiseError Err.Number, Err.Source, Routine_Name, Err.Description
End Function
Private Function ValidColumnDesignator( _
SearchTable As TableType, _
ByVal ColumnDesignator As Variant _
) As ColumnDesignatorType
' Purpose
' Determine if ColumnDesignator or "Empty" if valid
' Return the valid column name and number if valid
' Return ValidColumnDesignator.ColumnName = "Error Column Name Not Found" if invalid
' Assumptions
Const Routine_Name As String = Module_Name & "ValidColumnDesignator"
On Error GoTo ErrorHandler
' Verify that SearchTable is valid
If Left$(SearchTable.Valid, 5) = "Error" Then
ValidColumnDesignator.ColumnName = "Error Table"
Exit Function
End If
Dim ColumnFound As Boolean
Dim ColumnNumber As Long
Select Case VarType(ColumnDesignator)
Case vbInteger, vbLong
If ColumnDesignator >= LBound(SearchTable.Headers, 2) And _
ColumnDesignator <= UBound(SearchTable.Headers, 2) Then
ValidColumnDesignator.ColumnNumber = ColumnDesignator
Else
SearchTable.Valid = "Error Column Designator Out of Range"
End If
Case vbString
If ColumnDesignator = "Empty" Then
' "Empty" is a valid entry; means return an entire row
' Set ColumnNumber to 0 as a flag
ValidColumnDesignator.ColumnName = "Empty"
ValidColumnDesignator.ColumnNumber = 0
Else
ColumnFound = False
For ColumnNumber = 1 To UBound(SearchTable.Headers, 2)
If SearchTable.Headers(1, ColumnNumber) = ColumnDesignator Then
ColumnFound = True
ValidColumnDesignator.ColumnName = ColumnDesignator
ValidColumnDesignator.ColumnNumber = ColumnNumber
Exit For
End If
Next ColumnNumber
If Not ColumnFound Then
ValidColumnDesignator.ColumnName = "Error Column Name Not Found"
ValidColumnDesignator.ColumnNumber = 0
End If
End If
Case Else
SearchTable.Valid = "Error Invalid Column Name"
End Select
'@Ignore LineLabelNotUsed
Done:
Exit Function
ErrorHandler:
RaiseError Err.Number, Err.Source, Routine_Name, Err.Description
End Function
Private Function ValidFilter( _
SearchTable As TableType, _
ByVal ColumnFilter As String _
) As TableType
' Purpose
' Determines if the ColumnFilter is valid
' If ColumnFilter valid, returns the filtered array
' If ColumnFilter is invalid, returns ValidFilter.Valid to an error message
' Assumptions
' The structure of ColumnFilter is column name
' then a boolean comparator then the operand.
Const Routine_Name As String = Module_Name & "ValidFilter"
On Error GoTo ErrorHandler
' Verify that SearchTable is valid
If Left$(SearchTable.Valid, 5) = "Error" Then
ValidFilter.Valid = "Error Table"
Exit Function
End If
If ColumnFilter = "Empty" Then
' "Empty" is a valid ColumnFilter value
ValidFilter = SearchTable
Else
' Parse the ColumnFilter
Dim I As Long
Dim EndOfColumnName As Long
Dim StartOfOperand As Long
Dim PrevChar As String
Dim ThisChar As String
Dim NextChar As String
Dim Operator As String
Dim Operand As String
Dim ColumnName As String
Dim ColumnNumber As Long
For I = 1 To Len(ColumnFilter) - 1
' Iterate through ColumnFilter looking for "=", "<", or ">"
PrevChar = ThisChar
ThisChar = Mid$(ColumnFilter, I, 1)
NextChar = Mid$(ColumnFilter, I + 1, 1)
Select Case ThisChar
Case "="
Operator = "="
If PrevChar = " " Then
ColumnName = Mid$(ColumnFilter, 1, I - 2)
Else
ColumnName = Mid$(ColumnFilter, 1, I - 1)
End If
Dim TempValidColumnDesignator As ColumnDesignatorType
TempValidColumnDesignator = ValidColumnDesignator(SearchTable, ColumnName)
If TempValidColumnDesignator.ColumnName = "Error" Then
ValidFilter.Valid = "Error filter"
Exit Function
End If
ColumnNumber = TempValidColumnDesignator.ColumnNumber
If NextChar = " " Then
Operand = Mid$(ColumnFilter, I + 2, Len(ColumnFilter) - I - 1)
Else
Operand = Mid$(ColumnFilter, I + 1, Len(ColumnFilter) - I)
End If
Case "<" ' ThisChar = "<"
EndOfColumnName = I - 1
While Mid$(ColumnFilter, EndOfColumnName, 1) = " "
EndOfColumnName = EndOfColumnName - 1
Wend
Select Case NextChar
Case "="
Operator = "<="
StartOfOperand = I + 2
While Mid$(ColumnFilter, StartOfOperand, 1) = " "
StartOfOperand = StartOfOperand + 1
Wend
ColumnName = Mid$(ColumnFilter, 1, EndOfColumnName)
TempValidColumnDesignator = ValidColumnDesignator(SearchTable, ColumnName)
If TempValidColumnDesignator.ColumnName = "Error" Then
ValidFilter.Valid = "Error filter"
Exit Function
End If
ColumnNumber = TempValidColumnDesignator.ColumnNumber
Operand = Mid$(ColumnFilter, StartOfOperand, Len(ColumnFilter) - StartOfOperand + 1)
Exit For
Case ">"
Operator = "<>"
StartOfOperand = I + 2
While Mid$(ColumnFilter, StartOfOperand, 1) = " "
StartOfOperand = StartOfOperand + 1
Wend
ColumnName = Mid$(ColumnFilter, 1, EndOfColumnName)
TempValidColumnDesignator = ValidColumnDesignator(SearchTable, ColumnName)
If TempValidColumnDesignator.ColumnName = "Error" Then
ValidFilter.Valid = "Error filter"
Exit Function
End If
ColumnNumber = TempValidColumnDesignator.ColumnNumber
Operand = Mid$(ColumnFilter, StartOfOperand, Len(ColumnFilter) - StartOfOperand + 1)
Exit For
Case Else ' NextChar <> "=" and NextChar <> ">"
Operator = "<"
StartOfOperand = I + 1
While Mid$(ColumnFilter, StartOfOperand, 1) = " "
StartOfOperand = StartOfOperand + 1
Wend
ColumnName = Mid$(ColumnFilter, 1, EndOfColumnName)
TempValidColumnDesignator = ValidColumnDesignator(SearchTable, ColumnName)
If TempValidColumnDesignator.ColumnName = "Error" Then
Exit Function
End If
ColumnNumber = TempValidColumnDesignator.ColumnNumber
Operand = Mid$(ColumnFilter, StartOfOperand, Len(ColumnFilter) - StartOfOperand + 1)
Exit For
End Select
Case ">" ' ThisChar = ">"
EndOfColumnName = I - 1
While Mid$(ColumnFilter, EndOfColumnName, 1) = " "
EndOfColumnName = EndOfColumnName - 1
Wend
If NextChar = "=" Then
Operator = ">="
StartOfOperand = I + 2
While Mid$(ColumnFilter, StartOfOperand, 1) = " "
StartOfOperand = StartOfOperand + 1
Wend
ColumnName = Mid$(ColumnFilter, 1, EndOfColumnName)
TempValidColumnDesignator = ValidColumnDesignator(SearchTable, ColumnName)
If TempValidColumnDesignator.ColumnName = "Error" Then
ValidFilter.Valid = "Error filter"
Exit Function
End If
ColumnNumber = TempValidColumnDesignator.ColumnNumber
Operand = Mid$(ColumnFilter, StartOfOperand, Len(ColumnFilter) - StartOfOperand + 1)
Exit For
Else
Operator = ">"
StartOfOperand = I + 1
While Mid$(ColumnFilter, StartOfOperand, 1) = " "
StartOfOperand = StartOfOperand + 1
Wend
ColumnName = Mid$(ColumnFilter, 1, EndOfColumnName)
TempValidColumnDesignator = ValidColumnDesignator(SearchTable, ColumnName)
If TempValidColumnDesignator.ColumnName = "Error" Then
ValidFilter.Valid = "Error filter"
Exit Function
End If
ColumnNumber = TempValidColumnDesignator.ColumnNumber
Operand = Mid$(ColumnFilter, StartOfOperand, Len(ColumnFilter) - StartOfOperand + 1)
Exit For
End If
End Select
Next I
Dim FilterCriteria As String
FilterCriteria = " " & Operator & " " & """" & Operand & """"
ValidFilter = SetUpSearchTable(FilterCriteria, ColumnNumber)
If Left$(ValidFilter.Valid, 5) = "Error" Then
Exit Function
End If
End If ' ColumnFilter = "Empty"
'@Ignore LineLabelNotUsed
Done:
Exit Function
ErrorHandler:
RaiseError Err.Number, Err.Source, Routine_Name, Err.Description
End Function
Private Function SetUpSearchTable( _
ByVal FilterCriteria As String, _
ByVal FilterColumnNumber As Long _
) As TableType
' Purpose
' Filters Searchtable according to the FilterCriteria
' If ColumnFilter valid, returns the filtered array
' If ColumnFilter is invalid, sets SetUpSearchTable.Valid to an error message
' Assumptions
Const Routine_Name As String = Module_Name & "SetUpSearchTableColl"
On Error GoTo ErrorHandler
' Verify that SearchTable is valid
If Left$(pDataTable.Valid, 5) = "Error" Then
SetUpSearchTable.Valid = "Error Table"
Exit Function
End If
Dim I As Long
Dim SearchElement As SearchClass
Dim Expression As String
Dim SearchCollection As Collection
Set SearchCollection = New Collection
' Put all the rows that match FilterCriteris into a collection
For I = 1 To UBound(pDataTable.Body, 1)
Expression = Chr$(34) & pDataTable.Body(I, FilterColumnNumber) & Chr$(34) & FilterCriteria
If Evaluate(Expression) Then
Set SearchElement = New SearchClass
SearchElement.SetArray Me, I
SearchCollection.Add SearchElement
End If
Next I