-
Notifications
You must be signed in to change notification settings - Fork 0
/
CellBlock.cls
1530 lines (1085 loc) · 49 KB
/
CellBlock.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 = "CellBlock"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
''' Cell Block Class defines a cell block and lets you reach values in it
''' A Cell Block is a group of cells which can be defined as a table.
'''
''' Referencing from an initial cell, class finds the edges of the cell block and defines:
''' topRow: Range object which refers to top row of the cell block
''' bottomRow: Range object which refers to bottom row of the cell block
''' leftColumn: Range object which refers to left column of the cell block
''' rightColumn: Range object which refers to right column of the cell block
''' titleRow: Range object which refers to entire top row of the cell block.
''' titleColumn: Range object which refers to entire left column of the cell block
''' titleRowCells: Range object which refers to cell block part of the title row
''' titleColumnCells: Range object which refers to cell block part of the title column
''' top: Row number of the top row
''' left: Column number of the left column
''' width: Width of the cell block
''' height: Height of the cell block
''' size: Total number of cells in the cell block
''' activeRowCells: Dictionary object which let you reach any value in the selected row
''' using title of the row as key
''' Example:
''' Date A B C D
''' 20170101 1 2 3 ronaldinho
''' 20170102 [4] 5 6 messi
''' 20170103 7 8 9 c. ronaldo
'''
''' As 4 is selected, activeRowCells("D") gives you messi, activeRowCells("C") gives you 6
'''
''' activeColumnCells: Just like activeRowCells, but refers to cell values according to their
''' title columns
Option Explicit
Dim pActiveCell As Range, pFirstCell As Range, pLastCell As Range, _
pTopLeftCell As Range, pTopRightCell As Range, _
pBottomLeftCell As Range, pBottomRightCell As Range
Dim pActiveSheet As Worksheet
Dim pTopRow As Range, pBottomRow As Range, pLeftColumn As Range, pRightColumn As Range
Dim pTitleRow As Range, pTitleColumn As Range, pTitleRowCells As Range, pTitleColumnCells As Range
Dim pTop As Double, pLeft As Double, pWidth As Double, pHeight As Double, pSize As Double
Dim pActiveRowCells As Dictionary, pActiveColumnCells As Dictionary
Dim pRangeBlock As Range
Private Sub class_initialize()
' Set pActiveCell = Selection
' Set pActiveSheet = ActiveSheet
End Sub
Private Sub Class_Terminate()
On Error Resume Next
Set pActiveSheet = Nothing
Set pActiveRowCells = Nothing
Set pActiveColumnCells = Nothing
End Sub
' =========================================
' INITIATE OBJECT PROPERTIES
' -----------------------------------------
Public Sub InitiateProperties(Optional theSelection As Range, Optional theActiveSheet As Worksheet)
''' initiates cell block properties.
''' a remote cell or current selected cell can be assigned as a member of desired cell block
''' another worksheet or current worksheet can be assigned as desired worksheet
On Error Resume Next
' if theActiveSheet is not sent, assign current selected worksheet to the pActiveSheet
If theActiveSheet Is Nothing Then
Set pActiveSheet = activeSheet
Else
Set pActiveSheet = theActiveSheet
pActiveSheet.Activate
End If
' if selection is not sent, assign current selected cell to the pActiveCell
If theSelection Is Nothing Then
Set pActiveCell = Selection
Else
Set pActiveCell = theSelection
End If
Set pRangeBlock = pActiveCell
Call SetTopRow
Call SetBottomRow
Call SetLeftColumn
Call SetRightColumn
Set pTopLeftCell = activeSheet.Cells(pTopRow.Row, pLeftColumn.Column)
Set pTopRightCell = activeSheet.Cells(pTopRow.Row, pRightColumn.Column)
Set pBottomLeftCell = activeSheet.Cells(pBottomRow.Row, pLeftColumn.Column)
Set pBottomRightCell = activeSheet.Cells(pBottomRow.Row, pRightColumn.Column)
Set pFirstCell = pTopLeftCell
Set pLastCell = pBottomRightCell
'title row must be called after firstcell and lastcell are defined
Call SetTitleRow
Call SetTitleColumn
Call SetTitleRowCells
Call SetTitleColumnCells
pTop = pFirstCell.Row - 1
pLeft = pFirstCell.Column - 1
pHeight = pLastCell.Row - pFirstCell.Row + 1
pWidth = pLastCell.Column - pFirstCell.Column + 1
pSize = pHeight * pWidth
Call SetActiveRowCells
Call SetActiveColumnCells
End Sub
Public Sub InitiatePropertiesByConnectedCells(Optional theSelection As Range, Optional theActiveSheet As Worksheet)
''' !!! THIS SUB IS IN BETA, MAY CAUSE INFINITE LOOPS OR CRASH EXCEL !!!
''' initiates cell block properties.
''' a remote cell or current selected cell can be assigned as a member of desired cell block
''' another worksheet or current worksheet can be assigned as desired worksheet
On Error Resume Next
' if theActiveSheet is not sent, assign current selected worksheet to the pActiveSheet
If theActiveSheet Is Nothing Then
Set pActiveSheet = activeSheet
Else
Set pActiveSheet = theActiveSheet
pActiveSheet.Activate
End If
' if selection is not sent, assign current selected cell to the pActiveCell
If theSelection Is Nothing Then
Set pActiveCell = Selection
Else
Set pActiveCell = theSelection
End If
Call DefineByConnectedCells(theSelection)
Set pTopLeftCell = pActiveSheet.Cells(pTopRow.Row, pLeftColumn.Column)
Set pTopRightCell = pActiveSheet.Cells(pTopRow.Row, pRightColumn.Column)
Set pBottomLeftCell = pActiveSheet.Cells(pBottomRow.Row, pLeftColumn.Column)
Set pBottomRightCell = pActiveSheet.Cells(pBottomRow.Row, pRightColumn.Column)
Set pFirstCell = pTopLeftCell
Set pLastCell = pBottomRightCell
'title row must be called after firstcell and lastcell are defined
Call SetTitleRow
Call SetTitleColumn
Call SetTitleRowCells
Call SetTitleColumnCells
pTop = pFirstCell.Row - 1
pLeft = pFirstCell.Column - 1
pHeight = pLastCell.Row - pFirstCell.Row + 1
pWidth = pLastCell.Column - pFirstCell.Column + 1
pSize = pHeight * pWidth
Call SetActiveRowCells
Call SetActiveColumnCells
End Sub
' =========================================
' GETTERS
' -----------------------------------------
Public Property Get activeCell() As Range
Set activeCell = pActiveCell
End Property
Public Property Get topRow() As Range
Set topRow = pTopRow
End Property
Public Property Get bottomRow() As Range
Set bottomRow = pBottomRow
End Property
Public Property Get leftColumn() As Range
Set leftColumn = pLeftColumn
End Property
Public Property Get topLeftCell() As Range
Set topLeftCell = pTopLeftCell
End Property
Public Property Get topRightCell() As Range
Set topRightCell = pTopRightCell
End Property
Public Property Get bottomLeftCell() As Range
Set bottomLeftCell = pBottomLeftCell
End Property
Public Property Get bottomRightCell() As Range
Set bottomRightCell = pBottomRightCell
End Property
Public Property Get firstCell() As Range
Set firstCell = pFirstCell
End Property
Public Property Get lastCell() As Range
Set lastCell = pLastCell
End Property
Public Property Get top() As Double
top = pTop
End Property
Public Property Get Left() As Double
Left = pLeft
End Property
Public Property Get height() As Double
height = pHeight
End Property
Public Property Get size() As Double
size = pSize
End Property
Public Property Get CountA() As Double
for each cell in
End Property
Public Property Get width() As Double
width = pWidth
End Property
Public Property Get rangeBlock() As Range
Set rangeBlock = pRangeBlock
End Property
Public Property Get titleRow() As Range
Set titleRow = pTitleRow
End Property
Public Property Get titleColumn() As Range
Set titleColumn = pTitleColumn
End Property
Public Property Get titleRowCells() As Range
Set titleRowCells = pTitleRowCells
End Property
Public Property Get titleColumnCells() As Range
Set titleColumnCells = pTitleColumnCells
End Property
Public Property Get activeRowCells() As Dictionary
Set activeRowCells = pActiveRowCells
End Property
' =========================================
' NEW COLUMN
' -----------------------------------------
' NewColumn_Recordset is not written because it would be the same as NewRow_Recordset except for insert direction.
' While NewRow_Recordset shifts cells down in insert, NewColumn_Recordset should shift them right.
Public Sub NewColumn_Dictionary(theValues As Dictionary, Optional theColumnTitle As Variant = "", Optional isInsert As Boolean = False)
''' adds new column to current cell block
Dim key As Variant, rowCounter As Double, columnCounter As Double, rowCount As Double
columnCounter = 0
rowCount = 0
'find how many columns will be pasted
If theColumnTitle <> "" Then
rowCount = theValues.Count + 1
Else
rowCount = theValues.Count
End If
If isInsert Then
pActiveSheet.Range(Cells(pActiveCell.Row, pActiveCell.Column + 1), _
Cells(pActiveCell.Row + rowCount - 1, pActiveCell.Column + 1)). _
Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
pActiveSheet.Range(Cells(pActiveCell.Row, pActiveCell.Column + 1), _
Cells(pActiveCell.Row + rowCount - 1, pActiveCell.Column + 1)). _
Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End If
If theColumnTitle <> "" Then
pActiveSheet.Cells(pActiveCell.Row, pActiveCell.Column + columnCounter + 1) = theColumnTitle
rowCounter = 1
For Each key In theValues.Keys()
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + columnCounter) = key
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + columnCounter + 1) = theValues(key)
rowCounter = rowCounter + 1
Next key
Else
rowCounter = 0
For Each key In theValues.Keys()
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + columnCounter) = key
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + columnCounter + 1) = theValues(key)
rowCounter = rowCounter + 1
Next key
End If
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
Public Sub NewColumn_Collection(theValues As Collection, Optional theTitles As Collection, Optional theColumnTitle As Variant = "", Optional isInsert As Boolean = False)
''' writes new column
''' if theTitles is sent, it will write titles, too
Dim key As Variant, i As Integer, cell As Range, rowCount As Double, columnCounter As Double
rowCount = 0
columnCounter = 0
'find how many columns will be pasted
If theColumnTitle <> "" Then
rowCount = theValues.Count + 1
Else
rowCount = theValues.Count
End If
' if titles collection is sent, write both titles and values
If Not theTitles Is Nothing Then
' make two inserts
If isInsert Then
pActiveSheet.Range(Cells(pActiveCell.Row, pActiveCell.Column + 1), _
Cells(pActiveCell.Row + rowCount - 1, pActiveCell.Column + 1)). _
Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
pActiveSheet.Range(Cells(pActiveCell.Row, pActiveCell.Column + 1), _
Cells(pActiveCell.Row + rowCount - 1, pActiveCell.Column + 1)). _
Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End If
' if column title is sent
If theColumnTitle <> "" Then
pActiveSheet.Cells(pActiveCell.Row, pActiveCell.Column + columnCounter + 1) = theColumnTitle
' paste titles and values
For i = 1 To theTitles.Count
pActiveSheet.Cells(pActiveCell.Row + i, pActiveCell.Column + columnCounter) = theTitles(i)
pActiveSheet.Cells(pActiveCell.Row + i, pActiveCell.Column + columnCounter + 1) = theValues(i)
Next i
' if column title is not sent
Else
' paste titles and values
For i = 1 To theTitles.Count
pActiveSheet.Cells(pActiveCell.Row + i - 1, pActiveCell.Column + columnCounter) = theTitles(i)
pActiveSheet.Cells(pActiveCell.Row + i - 1, pActiveCell.Column + columnCounter + 1) = theValues(i)
Next i
End If
' if title is not sent,
Else
' make one insert
If isInsert Then
pActiveSheet.Range(Cells(pActiveCell.Row, pActiveCell.Column + 1), _
Cells(pActiveCell.Row + rowCount - 1, pActiveCell.Column + 1)). _
Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End If
' check if column title is sent
If theColumnTitle <> "" Then
pActiveSheet.Cells(pActiveCell.Row, pActiveCell.Column + columnCounter) = theColumnTitle
For i = 1 To theValues.Count
pActiveSheet.Cells(pActiveCell.Row + i, pActiveCell.Column + columnCounter) = theValues(i)
Next i
Else
For i = 1 To theValues.Count
pActiveSheet.Cells(pActiveCell.Row + i - 1, pActiveCell.Column + columnCounter) = theValues(i)
Next i
End If
End If
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
' =========================================
' NEW ROW
' -----------------------------------------
Public Sub NewRow_Recordset(theValues As ADODB.Recordset, Optional isInsert As Boolean = False)
''' adds new row to current cell block
''' looks at the titles of the current cell block and inserts new rows according to them.
''' if the cell block is empty, inserts nothing
Dim i As Double, rowCounter As Double, columnCount As Double, columnCounter As Double
' for the key values of theValues which doesn't exist in pActiveRowCells, put an error handling
On Error Resume Next
If theValues.EOF Then theValues.MoveFirst
columnCount = 0
rowCounter = 0
columnCounter = 0
'find how many columns will be pasted
columnCount = theValues.fields.Count
' ===== write titles =====
If isInsert Then
pActiveSheet.Range(Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column), _
Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column + columnCount - 1)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
For i = 0 To theValues.fields.Count - 1
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + columnCounter) = theValues.fields(i).Name
columnCounter = columnCounter + 1
Next i
rowCounter = rowcountern + 1
' ===== write values =====
Do Until theValues.EOF
' insert for each row
If isInsert Then
pActiveSheet.Range(Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column), _
Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column + columnCount - 1)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
columnCounter = 0
For i = 0 To theValues.fields.Count - 1
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + columnCounter) = theValues.fields(i).Value
columnCounter = columnCounter + 1
Next i
rowCounter = rowCounter + 1
theValues.MoveNext
Loop
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
Public Sub NewRow_Dictionary(theValues As Dictionary, Optional theRowTitle As Variant = "", Optional isInsert As Boolean = False)
''' creates new cell block
Dim key As Variant, rowCounter As Double, columnCount As Double, columnCounter As Double
columnCount = 0
rowCounter = 0
'find how many columns will be pasted
If theRowTitle <> "" Then
columnCount = theValues.Count + 1
Else
columnCount = theValues.Count
End If
If isInsert Then
pActiveSheet.Range(Cells(pActiveCell.Row + 1, pActiveCell.Column), _
Cells(pActiveCell.Row + 1, pActiveCell.Column + columnCount - 1)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
pActiveSheet.Range(Cells(pActiveCell.Row + 1, pActiveCell.Column), _
Cells(pActiveCell.Row + 1, pActiveCell.Column + columnCount - 1)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
If theRowTitle <> "" Then
pActiveSheet.Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column) = theRowTitle
columnCounter = 1
For Each key In theValues.Keys()
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + columnCounter) = key
pActiveSheet.Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column + columnCounter) = theValues(key)
columnCounter = columnCounter + 1
Next key
Else
columnCounter = 0
For Each key In theValues.Keys()
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + columnCounter) = key
pActiveSheet.Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column + columnCounter) = theValues(key)
columnCounter = columnCounter + 1
Next key
End If
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
Public Sub NewRow_Collection(theValues As Collection, Optional theTitles As Collection, Optional theRowTitle As Variant = "", Optional isInsert As Boolean = False)
''' writes new row
''' if theTitles is sent, it will write titles, too
Dim key As Variant, i As Integer, cell As Range, rowCounter As Double, columnCount As Double
rowCounter = 0
columnCount = 0
'find how many columns will be pasted
If theRowTitle <> "" Then
columnCount = theValues.Count + 1
Else
columnCount = theValues.Count
End If
' if titles collection is sent, write both titles and values
If Not theTitles Is Nothing Then
' make two inserts
If isInsert Then
pActiveSheet.Range(Cells(pActiveCell.Row + 1, pActiveCell.Column), _
Cells(pActiveCell.Row + 1, pActiveCell.Column + columnCount - 1)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
pActiveSheet.Range(Cells(pActiveCell.Row + 1, pActiveCell.Column), _
Cells(pActiveCell.Row + 1, pActiveCell.Column + columnCount - 1)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
' if row title is sent
If theRowTitle <> "" Then
pActiveSheet.Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column) = theRowTitle
' paste titles and values
For i = 1 To theTitles.Count
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + i) = theTitles(i)
pActiveSheet.Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column + i) = theValues(i)
Next i
' if row title is not sent
Else
' paste titles and values
For i = 1 To theTitles.Count
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + i - 1) = theTitles(i)
pActiveSheet.Cells(pActiveCell.Row + rowCounter + 1, pActiveCell.Column + i - 1) = theValues(i)
Next i
End If
' if title is not sent,
Else
' make two inserts
If isInsert Then
pActiveSheet.Range(Cells(pActiveCell.Row + 1, pActiveCell.Column), _
Cells(pActiveCell.Row + 1, pActiveCell.Column + columnCount - 1)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
' check if row title is sent
If theRowTitle <> "" Then
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column) = theRowTitle
For i = 1 To theValues.Count
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + i) = theValues(i)
Next i
Else
For i = 1 To theValues.Count
pActiveSheet.Cells(pActiveCell.Row + rowCounter, pActiveCell.Column + i - 1) = theValues(i)
Next i
End If
End If
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
' =========================================
' ADD ROW
' -----------------------------------------
' PASSED
Public Sub AddRow_Recordset(theValues As ADODB.Recordset, Optional isInsert As Boolean = False)
''' adds new row to current cell block
''' looks at the titles of the current cell block and inserts new rows according to them.
''' if the cell block is empty, inserts nothing
Dim i As Double, rowCounter As Double
' for the key values of theValues which doesn't exist in pActiveRowCells, put an error handling
On Error Resume Next
If theValues.EOF Then theValues.MoveFirst
rowCounter = 1
Do Until theValues.EOF
' insert for each row
If isInsert Then
pActiveSheet.Range(Cells(pBottomLeftCell.Row + rowCounter, pBottomLeftCell.Column), _
Cells(pBottomRightCell.Row + rowCounter, pBottomRightCell.Column)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
For i = 0 To theValues.fields.Count - 1
pActiveSheet.Cells(pLastCell.Row + rowCounter, pActiveRowCells(theValues.fields(i).Name).Column) = theValues.fields(i).Value
Next i
rowCounter = rowCounter + 1
theValues.MoveNext
Loop
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
' PASSED
Public Sub AddRow_Dictionary(theValues As Dictionary, Optional theRowTitle As Variant = "", Optional isInsert As Boolean = False)
''' adds new row to current cell block
Dim key As Variant, rowCounter As Double
If isInsert Then
pActiveSheet.Range(Cells(pBottomLeftCell.Row + 1, pBottomLeftCell.Column), _
Cells(pBottomRightCell.Row + 1, pBottomRightCell.Column)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
rowCounter = 1
If theRowTitle <> "" Then
pActiveSheet.Cells(pBottomLeftCell.Row + 1, pTitleColumn.Column) = theRowTitle
End If
' for the key values of theValues which doesn't exist in pActiveRowCells, put an error handling
On Error Resume Next
For Each key In theValues.Keys()
pActiveSheet.Cells(pLastCell.Row + rowCounter, pActiveRowCells(key).Column) = theValues(key)
Next key
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
Public Sub AddRow_Collection(theValues As Collection, Optional theTitles As Collection, Optional theRowTitle As Variant = "", Optional isInsert As Boolean = False)
''' adds new row to current cell block
''' if theTitles is sent, it will find the column of corresponding title and add values accordingly
''' otherwise, it will begin adding adding row title and values from the first colum. if there is no row title,
''' it will begin adding values only from the first column
Dim key As Variant, i As Integer, cell As Range, rowCounter As Double
rowCounter = 1
If isInsert Then
pActiveSheet.Range(Cells(pBottomLeftCell.Row + 1, pBottomLeftCell.Column), _
Cells(pBottomRightCell.Row + 1, pBottomRightCell.Column)). _
Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
If theRowTitle <> "" Then
pActiveSheet.Cells(pBottomLeftCell.Row + rowCounter, pTitleColumn.Column) = theRowTitle
End If
' if title collection is sent, find columns and add new row
If Not theTitles Is Nothing Then
' paste row according to titles in the existing cell block
For i = 1 To theTitles.Count
For Each cell In pTitleRowCells
If CStr(cell.Value) = theTitles(i) Then
pActiveSheet.Cells(pLastCell.Row + rowCounter, cell.Column) = theValues(i)
End If
Next cell
Next i
' if title is not sent,
Else
' check if row title is sent
If theRowTitle <> "" Then
For i = 1 To theValues.Count
pActiveSheet.Cells(pLastCell.Row + rowCounter, pFirstCell.Column + i) = theValues(i)
Next i
Else
For i = 1 To theValues.Count
pActiveSheet.Cells(pLastCell.Row + rowCounter, pFirstCell.Column - 1 + i) = theValues(i)
Next i
End If
End If
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
' =========================================
' ADD COLUMN
' -----------------------------------------
Public Sub AddColumn_Recordset(theValues As ADODB.Recordset, Optional isInsert As Boolean = False)
''' adds new row to current cell block
Dim i As Double, rowCounter As Double
' if isInsert, insert number of columns
If isInsert Then
For i = 0 To theValues.fields.Count - 1
pActiveSheet.Range(Cells(pTopRightCell.Row, pTopRightCell.Column + 1), _
Cells(pBottomRightCell.Row, pBottomRightCell.Column + 1)). _
Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Next i
End If
' enter titles
For i = 0 To theValues.fields.Count - 1
pActiveSheet.Cells(pTitleRow.Row, pRightColumn.Column + i + 1) = theValues.fields(i).Name
Next i
' enter values
On Error Resume Next
If theValues.EOF Then theValues.MoveFirst
rowCounter = 1
Do Until theValues.EOF
' if record count is more than height of cell block, insert for each record seperately
If pTitleRow.Row + rowCounter > pBottomRow.Row Then
If isInsert Then
pActiveSheet.Range(Cells(pTitleRow.Row + rowCounter, pTopRightCell.Column + 1), _
Cells(pTitleRow.Row + rowCounter, pBottomRightCell.Column + theValues.fields.Count)). _
Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End If
End If
For i = 0 To theValues.fields.Count - 1
pActiveSheet.Cells(pTitleRow.Row + rowCounter, pRightColumn.Column + i + 1) = theValues.fields(i).Value
Next i
rowCounter = rowCounter + 1
theValues.MoveNext
Loop
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
Public Sub AddColumn_Dictionary(theValues As Dictionary, Optional theColumnTitle As Variant = "", Optional isInsert As Boolean = False)
''' adds new column to current cell block
Dim key As Variant
If isInsert Then
pActiveSheet.Range(Cells(pTopRightCell.Row, pTopRightCell.Column + 1), _
Cells(pBottomRightCell.Row, pBottomRightCell.Column + 1)). _
Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End If
If theColumnTitle <> "" Then
pActiveSheet.Cells(pTitleRow.Row, pTopRightCell.Column + 1) = theColumnTitle
End If
' for the key values of theValues which doesn't exist in pActiveColumnCells, put an error handling
On Error Resume Next
For Each key In theValues.Keys()
pActiveSheet.Cells(pActiveColumnCells(key).Row, pLastCell.Column + 1) = theValues(key)
Next key
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
Public Sub AddColumn_Collection(theValues As Collection, Optional theTitles As Collection, Optional theColumnTitle As Variant = "", Optional isInsert As Boolean = False)
''' adds new Column to current cell block
''' if theTitles is sent, it will find the row of corresponding title and add values accordingly
''' otherwise, it will begin adding adding row title and values from the first colum. if there is no row title,
''' it will begin adding values only from the first column
Dim key As Variant, i As Integer, cell As Range
If isInsert Then
pActiveSheet.Range(Cells(pTopRightCell.Row, pTopRightCell.Column + 1), _
Cells(pBottomRightCell.Row, pBottomRightCell.Column + 1)). _
Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End If
If theColumnTitle <> "" Then
pActiveSheet.Cells(pTitleRow.Row, pLastCell.Column + 1) = theColumnTitle
End If
' if title collection is sent, find columns and add new Column
If Not theTitles Is Nothing Then
For i = 1 To theTitles.Count
For Each cell In pTitleColumnCells
If CStr(cell.Value) = theTitles(i) Then
pActiveSheet.Cells(cell.Row, pLastCell.Column + 1) = theValues(i)
End If
Next cell
Next i
' if title is not sent,
Else
' check if title is sent
If theColumnTitle <> "" Then
For i = 1 To theValues.Count
pActiveSheet.Cells(pTitleRow.Row + i, pLastCell.Column + 1) = theValues(i)
Next i
Else
For i = 1 To theValues.Count
pActiveSheet.Cells(pTitleRow.Row - 1 + i, pLastCell.Column + 1) = theValues(i)
Next i
End If
End If
' modify cell block to be fit with the new row
Call InitiateProperties
End Sub
' =========================================
' PUBLIC FUNCTIONS
' -----------------------------------------
Public Function isEdgeCell(theRange As Range) As Boolean
''' check if the cell is located in the edge of a cell block
' if the cell is at the edge of the spreadsheet, return true
If theRange.Row - 1 = 0 Or theRange.Row + 1 = 1048577 Then
isEdgeCell = True
Exit Function
End If
If theRange.Column - 1 = 0 Or theRange.Column + 1 = 16385 Then
isEdgeCell = True
Exit Function
End If
' check if surrounding cells are empty if any one of them is empty, then this is an edge cell
If IsEmpty(theRange.Offset(-1, 0)) Then
isEdgeCell = True
Exit Function
End If
If IsEmpty(theRange.Offset(-1, 1)) Then
isEdgeCell = True
Exit Function
End If
If IsEmpty(theRange.Offset(0, 1)) Then
isEdgeCell = True
Exit Function
End If
If IsEmpty(theRange.Offset(1, 1)) Then
isEdgeCell = True
Exit Function
End If
If IsEmpty(theRange.Offset(1, 0)) Then
isEdgeCell = True
Exit Function
End If
If IsEmpty(theRange.Offset(1, -1)) Then
isEdgeCell = True
Exit Function
End If
If IsEmpty(theRange.Offset(0, -1)) Then
isEdgeCell = True
Exit Function
End If
If IsEmpty(theRange.Offset(-1, -1)) Then
isEdgeCell = True
Exit Function
End If
isEdgeCell = False
End Function
' =========================================
' SET ATTRIBUTES
' -----------------------------------------
Private Sub SetActiveRowCells()
On Error Resume Next
Dim cell As Range