-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayHelper.bas
More file actions
834 lines (637 loc) · 25.5 KB
/
ArrayHelper.bas
File metadata and controls
834 lines (637 loc) · 25.5 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
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
Attribute VB_Name = "ArrayHelper"
'---------------------------------------------------------------------------------------
' Module : ArrayHelper
' Author : KRISH J
' Date : 23/09/2019
' Purpose :
' Returns :
' : 23-09-2019: Remove duplicates added
' ArrayStringArrayPointer and arrayUtrArrayPointer function moved from system module to arrayhelper module
' arrayExists reneamed to arrayExistsByMemRef
' 07/12/2019: some functions taken from https://github.com/todar/VBA-Arrays/blob/master/ArrayFunctions.bas
' 31/01/2020: FnArrayHasItemAt added. FnArrayGetSize improved
' 02/06/2020: FnArrayGetSize bug corrected regarding 0 index with data
' ArrayPush and pushTop can add empty items
'---------------------------------------------------------------------------------------
Option Compare Database
Option Explicit
'ERROR CODES CONSTANTS
Public Const ARRAY_NOT_PASSED_IN As Integer = 5000
Public Const ARRAY_DIMENSION_INCORRECT As Integer = 5001
Public Function ArrayAddString(ByRef stringArray As Variant, stringValue As String)
'Adds a string to an existing string array
On Error Resume Next
If IsBlank(stringArray) Then Exit Function
'If stringValue = "" Then Exit Function 'Allow empty string
Dim L As Long
L = FnArrayGetSize(stringArray)
INC L
ReDim Preserve stringArray(L)
stringArray(L - 1) = stringValue
End Function
Public Function ArrayContainsEmpties(ByVal sourceArray As Variant) As Boolean
'CHECK TO SEE IF SINGLE DIM ARRAY CONTAINS ANY EMPTY INDEXES
'THIS FUNCTION IS FOR SINGLE DIMS ONLY
If ArrayDimensionLength(sourceArray) <> 1 Then
err.Raise ARRAY_DIMENSION_INCORRECT, , "SourceArray must be a single dimensional array."
End If
Dim index As Integer
For index = LBound(sourceArray, 1) To UBound(sourceArray, 1)
If IsEmpty(sourceArray(index)) Then
ArrayContainsEmpties = True
Exit Function
End If
Next index
End Function
Function ArrayConvertVariantToStringArray(iVariantArray As Variant) As String()
'---------------------------------------------------------------------------------------
' Procedure : ArrayConvertVariantToStringArray
' Author : KRISH J
' Date : 23/09/2019
' Purpose : converts a variant array to string array
' Returns :
' Usage :
'---------------------------------------------------------------------------------------
'
'Does source array has any entries?
If ArrayHelper.ArrayIsEmpty(iVariantArray) Then Exit Function
Dim stringArray() As String
Dim arraySize As Long
arraySize = ArrayHelper.FnArrayGetSize(iVariantArray)
'Make new holder
ReDim stringArray(arraySize) As String
Dim I As Long
For I = 0 To arraySize - 1
stringArray(I) = CStr(iVariantArray(I))
Next
ArrayConvertVariantToStringArray = stringArray
End Function
Public Function ArrayDimensionLength(ByVal sourceArray As Variant) As Long
' Returns the length of the dimension of an array.
On Error GoTo Catch
Do
Dim boundIndex As Long
boundIndex = boundIndex + 1
' Loop until this line errors out.
Dim test As Long
test = UBound(sourceArray, boundIndex)
Loop
Catch:
' Must remove one, this gives the proper dimension length.
ArrayDimensionLength = boundIndex - 1
End Function
Public Function ArrayExistsByMemRef(ByVal ppArray As Long) As Long
GetMem4 ppArray, VarPtr(ArrayExistsByMemRef)
'Print ArrayExists(ArrPtr(someArray))
'Print ArrayExists(StrArrPtr(someArrayOfStrings))
'Print ArrayExists(UDTArrPtr(someArrayOfUDTs))
End Function
Public Function ArrayExtractColumn(ByVal sourceArray As Variant, ByVal ColumnIndex As Integer) As Variant
'GET A COLUMN FROM A TWO DIM ARRAY, AND RETURN A SINLGE DIM ARRAY
'SINGLE DIM ARRAYS ONLY
If ArrayDimensionLength(sourceArray) <> 2 Then
err.Raise ARRAY_DIMENSION_INCORRECT, , "SourceArray must be a two dimensional array."
End If
Dim Temp As Variant
ReDim Temp(LBound(sourceArray, 1) To UBound(sourceArray, 1))
Dim RowIndex As Integer
For RowIndex = LBound(sourceArray, 1) To UBound(sourceArray, 1)
Temp(RowIndex) = sourceArray(RowIndex, ColumnIndex)
Next RowIndex
ArrayExtractColumn = Temp
End Function
Public Function ArrayExtractRow(ByVal sourceArray As Variant, ByVal RowIndex As Long) As Variant
'GET A ROW FROM A TWO DIM ARRAY, AND RETURN A SINLGE DIM ARRAY
Dim Temp As Variant
ReDim Temp(LBound(sourceArray, 2) To UBound(sourceArray, 2))
Dim ColIndex As Integer
For ColIndex = LBound(sourceArray, 2) To UBound(sourceArray, 2)
Temp(ColIndex) = sourceArray(RowIndex, ColIndex)
Next ColIndex
ArrayExtractRow = Temp
End Function
Public Function ArrayFromRecordset(rs As Object, Optional IncludeHeaders As Boolean = True) As Variant
'RETURNS A 2D ARRAY FROM A RECORDSET, OPTIONALLY INCLUDING HEADERS, AND IT TRANSPOSES TO KEEP
'ORIGINAL OPTION BASE. (TRANSPOSE WILL SET IT TO BASE 1 AUTOMATICALLY.)
'@NOTE: -Int(IncludeHeaders) RETURNS A BOOLEAN TO AN INT (0 OR 1)
Dim HeadingIncrement As Integer
HeadingIncrement = -Int(IncludeHeaders)
'CHECK TO MAKE SURE THERE ARE RECORDS TO PULL FROM
If rs.BOF Or rs.EOF Then
Exit Function
End If
'STORE RS DATA
Dim rsData As Variant
rsData = rs.GetRows
'REDIM TEMP TO ALLOW FOR HEADINGS AS WELL AS DATA
Dim Temp As Variant
ReDim Temp(LBound(rsData, 2) To UBound(rsData, 2) + HeadingIncrement, LBound(rsData, 1) To UBound(rsData, 1))
If IncludeHeaders = True Then
'GET HEADERS
Dim headerIndex As Long
For headerIndex = 0 To rs.Fields.Count - 1
Temp(LBound(Temp, 1), headerIndex) = rs.Fields(headerIndex).name
Next headerIndex
End If
'GET DATA
Dim RowIndex As Long
Dim ColIndex As Long
For RowIndex = LBound(Temp, 1) + HeadingIncrement To UBound(Temp, 1)
For ColIndex = LBound(Temp, 2) To UBound(Temp, 2)
Temp(RowIndex, ColIndex) = rsData(ColIndex, RowIndex - HeadingIncrement)
Next ColIndex
Next RowIndex
'RETURN
ArrayFromRecordset = Temp
End Function
Public Function ArrayGetValues(ByRef stringArray As Variant, Optional delimitter As String = ",")
'returns array values in a single line
Dim I As Long
For I = 0 To UBound(stringArray) - 1
If (I = 0) Then
ArrayGetValues = ArrayGetValues & stringArray(I)
Else
ArrayGetValues = ArrayGetValues & "," & stringArray(I)
End If
Next I
End Function
Public Function ArrayIndexOf(ByVal sourceArray As Variant, ByVal SearchElement As Variant) As Integer
'RETURNS INDEX OF A SINGLE DIM ARRAY ELEMENT
If ArrayIsEmpty(sourceArray) Then Exit Function
Dim index As Long
For index = LBound(sourceArray, 1) To UBound(sourceArray, 1)
If sourceArray(index) = SearchElement Then
ArrayIndexOf = index
Exit Function
End If
Next index
index = -1
End Function
Public Function ArrayIsEmpty(ByRef sourceArray) As Boolean
'a modified version of cpearsons code <http://www.cpearson.com/excel/VBAArrays.htm>
' Array was not passed in.
If Not IsArray(sourceArray) Then
ArrayIsEmpty = True
Exit Function
End If
' Attempt to get the UBound of the array. If the array is
' unallocated, an error will occur.
err.Clear
On Error Resume Next
If Not IsNumeric(UBound(sourceArray)) And (err.number <> 0) Then
ArrayIsEmpty = True
Else
' On rare occasion Err.Number will be 0 for an unallocated, empty array.
' On these occasions, LBound is 0 and UBound is -1.
' To accommodate the weird behavior, test to see if LB > UB. If so, the array is not allocated.
err.Clear
If LBound(sourceArray) > UBound(sourceArray) Then
ArrayIsEmpty = True
End If
End If
End Function
Function ArrayRemoveDuplicates(iArray As Variant) As Variant
'DESCRIPTION: Removes duplicates from your array using the dictionary method.
'NOTES: (1.a) You must add a reference to the Microsoft Scripting Runtime library via
' the Tools > References menu.
' (1.b) This is necessary because I use Early Binding in this function.
' Early Binding greatly enhances the speed of the function.
' (2) The scripting dictionary will not work on the Mac OS.
'SOURCE: https://wellsr.com
'-----------------------------------------------------------------------
If ArrayHelper.ArrayIsEmpty(iArray) Then Exit Function
Dim I As Long
Dim d As Scripting.Dictionary
Set d = New Scripting.Dictionary
With d
For I = LBound(iArray) To UBound(iArray)
If IsMissing(iArray(I)) = False Then
.Item(iArray(I)) = 1
End If
Next
ArrayRemoveDuplicates = .Keys
End With
End Function
Public Function FnArrayRemoveDuplicates(ByVal sourceArray As Variant)
'Removes duplicates in an array
'taken from : https://stackoverflow.com/questions/11870095/remove-duplicates-from-array-using-vba
Dim poArrNoDup()
Dim dupArrIndex As Long
Dim I As Long
Dim J As Long
Dim dupBool As Boolean
dupArrIndex = -1
For I = LBound(sourceArray) To UBound(sourceArray)
dupBool = False
For J = LBound(sourceArray) To I
If sourceArray(I) = sourceArray(J) And Not I = J Then
dupBool = True
Exit For
End If
Next J
If dupBool = False Then
dupArrIndex = dupArrIndex + 1
ReDim Preserve poArrNoDup(dupArrIndex)
poArrNoDup(dupArrIndex) = sourceArray(I)
End If
Next I
FnArrayRemoveDuplicates = poArrNoDup
End Function
Public Function ArraySort(sourceArray As Variant) As Variant
'SORT AN ARRAY [SINGLE DIMENSION]
'SORT ARRAY A-Z
Dim OuterIndex As Long
For OuterIndex = LBound(sourceArray) To UBound(sourceArray) - 1
Dim InnerIndex As Long
For InnerIndex = OuterIndex + 1 To UBound(sourceArray)
If sourceArray(OuterIndex) > sourceArray(InnerIndex) Then
Dim Temp As Variant
Temp = sourceArray(InnerIndex)
sourceArray(InnerIndex) = sourceArray(OuterIndex)
sourceArray(OuterIndex) = Temp
End If
Next InnerIndex
Next OuterIndex
ArraySort = sourceArray
End Function
Public Function FnArrayQuickSort(ByVal vArray As Variant, inLow As Long, inHi As Long)
'Taken from https://stackoverflow.com/questions/152319/vba-array-sort-function
Dim pivot As Variant
Dim tmpSwap As Variant
Dim tmpLow As Long
Dim tmpHi As Long
tmpLow = inLow
tmpHi = inHi
pivot = vArray((inLow + inHi) \ 2)
While (tmpLow <= tmpHi)
While (vArray(tmpLow) < pivot And tmpLow < inHi)
tmpLow = tmpLow + 1
Wend
While (pivot < vArray(tmpHi) And tmpHi > inLow)
tmpHi = tmpHi - 1
Wend
If (tmpLow <= tmpHi) Then
tmpSwap = vArray(tmpLow)
vArray(tmpLow) = vArray(tmpHi)
vArray(tmpHi) = tmpSwap
tmpLow = tmpLow + 1
tmpHi = tmpHi - 1
End If
Wend
If (inLow < tmpHi) Then vArray = FnArrayQuickSort(vArray, inLow, tmpHi)
If (tmpLow < inHi) Then vArray = FnArrayQuickSort(vArray, tmpLow, inHi)
End Function
Public Function ArrayStringArrayPointer(arr() As String, Optional ByVal IgnoreMe As Long = 0) As Long
GetMem4 VarPtr(IgnoreMe) - 4, VarPtr(ArrayStringArrayPointer)
End Function
Public Sub ArrayToTextFile(arr As Variant, filePath As String, Optional delimeter As String = ",")
'SENDS AN ARRAY TO A TEXTFILE
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Ts As Object
Set Ts = fso.OpenTextFile(filePath, 2, True) '2=WRITEABLE
Ts.Write FnArrayToString(arr, delimeter)
Set Ts = Nothing
Set fso = Nothing
End Sub
Public Function ArrayTranspose(sourceArray As Variant) As Variant
'APPLICATION.TRANSPOSE HAS A LIMIT ON THE SIZE OF THE ARRAY, AND IS LIMITED TO THE 1ST DIM
Dim Temp As Variant
Select Case ArrayDimensionLength(sourceArray)
Case 2:
ReDim Temp(LBound(sourceArray, 2) To UBound(sourceArray, 2), LBound(sourceArray, 1) To UBound(sourceArray, 1))
Dim I As Long
Dim J As Long
For I = LBound(sourceArray, 2) To UBound(sourceArray, 2)
For J = LBound(sourceArray, 1) To UBound(sourceArray, 1)
Temp(I, J) = sourceArray(J, I)
Next
Next
End Select
ArrayTranspose = Temp
sourceArray = Temp
End Function
Public Function ArrayUDTArraryPointer(ByRef arr As Variant) As Long
If varType(arr) Or vbArray Then
GetMem4 VarPtr(arr) + 8, VarPtr(ArrayUDTArraryPointer)
Else
err.Raise 5, , "Variant must contain array of user defined type"
End If
End Function
Public Function ArrayUnShift(ByRef byrefArray, ByVal Item)
'Adds new item to the top of the array
ArrayHelper.FnArrayPushToTop byrefArray, Item
ArrayUnShift = byrefArray
End Function
Public Function Assign(ByRef Variable As Variant, ByVal value As Variant) As String
' Quick tool to either set or let depending on if the element is an object
If IsObject(value) Then
Set Variable = value
Else
Let Variable = value
End If
Assign = TypeName(value)
End Function
Public Function ConvertToArray(ByRef Val As Variant) As Variant
'CONVERT OTHER LIST OBJECTS TO AN ARRAY
Select Case TypeName(Val)
Case "Collection":
Dim index As Integer
For index = 1 To Val.Count
FnArrayPush ConvertToArray, Val(index)
Next index
Case "Dictionary":
ConvertToArray = Val.items()
Case Else
If IsArray(Val) Then
ConvertToArray = Val
Else
FnArrayPush ConvertToArray, Val
End If
End Select
End Function
Public Function FnArrayAddItem(ByRef byrefArray, ByVal iItem)
'Adds an item to the end of the array
FnArrayPush byrefArray, iItem
FnArrayAddItem = byrefArray
End Function
Public Function FnArrayContainsItem(ByRef iArray, Item As Variant) As Boolean
'Returns true if the array contains the item provided, otherwise false
'Array has any item at all?
If Not ArrayHelper.FnArrayHasitem(iArray) Then
Exit Function
End If
'loop through array and find a match
Dim v As Variant
For Each v In iArray
If v = Item Then
FnArrayContainsItem = True
Exit For
End If
Next v
End Function
Public Function FnArrayGetSize(ByRef byrefArray) As Long
'Returns the size of an array or 0
'if the first or last item contains vbNullstring, that item is discarted
'16/05/2020: Redim will add blank item at the end of array. Check for data at index = 0
Dim T As Long
Dim v As Variant
Dim hasValue As Boolean
On Error Resume Next
T = UBound(byrefArray, 1) ' - LBound(byrefArray, 1) + 1
' 'Inline arrays have ubound = 0 but will have an item at 0 index
' If (t = 0) Then
' hasValue = ((Not byrefArray(0) Is Nothing) And byrefArray(0) <> vbNullString)
' If (hasValue) Then
' t = 1
' End If
' End If
'
'Some array have array size of 0 but contains an item at the 0 index. Check if array size returned from ubound contains a value. if yes increase the size by one
If T = 0 And ArrayHelper.FnArrayHasItemAt(byrefArray, T) Then
Select Case varType(byrefArray(T))
Case vbArray
' Array contains item?
hasValue = FnArrayHasItemAt(byrefArray, T)
Case vbBoolean
hasValue = Not IsBlank(byrefArray(T))
Case vbString
hasValue = byrefArray(T) <> vbNullString
Case vbObject
hasValue = Not byrefArray(T) Is Nothing
Case Else
hasValue = False
End Select
' hasValue = (ArrayHelper.FnArrayHasItemAt(byrefArray, t) _
' And Not (byrefArray(t) = vbNullString) _
' And Not ((varType(byrefArray(t)) = vbArray) And (FnArrayHasItemAt(byrefArray, t))) _
' And Not ((varType(byrefArray(t)) = vbBoolean) And (byrefArray(t) = False)) _
' And Not ((varType(byrefArray(t)) = vbByte) And (byrefArray(t) = 0)) _
' And Not ((varType(byrefArray(t)) = vbCurrency) And (byrefArray(t) = 0)) _
' And Not ((varType(byrefArray(t)) = vbDataObject) And (byrefArray(t) Is Nothing)) _
' And Not ((varType(byrefArray(t)) = vbDate) And (byrefArray(t) = 0)) _
' And Not ((varType(byrefArray(t)) = vbDecimal) And (byrefArray(t) = 0)) _
' And Not ((varType(byrefArray(t)) = vbDouble) And (byrefArray(t) = 0)) _
' And Not ((varType(byrefArray(t)) = vbEmpty) And (byrefArray(t) = vbEmpty)) _
' And Not ((varType(byrefArray(t)) = vbError) And (byrefArray(t) = 0)) _
' And Not ((varType(byrefArray(t)) = vbInteger) And (byrefArray(t) = 0)) _
' And Not ((varType(byrefArray(t)) = vbLong) And (byrefArray(t) = 0)) _
' And Not ((varType(byrefArray(t)) = vbNull) And (byrefArray(t) Is Null)) _
' And Not ((varType(byrefArray(t)) = vbObject) And (byrefArray(t) Is Nothing)) _
' And Not ((varType(byrefArray(t)) = vbSingle) And (byrefArray(t) = 0)) _
' And Not ((varType(byrefArray(t)) = vbString) And (byrefArray(t) = vbNullString)) _
' And Not ((varType(byrefArray(t)) = vbUserDefinedType) And (byrefArray(t) Is Nothing)) _
' And Not ((varType(byrefArray(t)) = vbVariant) And (IsBlank(byrefArray(t)))) _
' )
If (hasValue) Then
INC T
End If
End If
'
If T < 0 Then
T = 0
End If
FnArrayGetSize = T
End Function
Public Function FnArrayHasitem(ByRef iArray) As Boolean
'Has this array contain any item?
' todo: rename this function
On Error Resume Next
FnArrayHasitem = Not IsBlank(iArray(0))
End Function
Public Function FnArrayHasItemAt(ByRef iArray, ByVal index As Long) As Boolean
'Does this array has an item at given index? item can be blank.
On Error GoTo IndexError
Dim v As Variant
If (varType(iArray(index)) = vbObject) Then
Set v = iArray(index)
Else
v = iArray(index)
End If
FnArrayHasItemAt = True
ExitRoutine:
On Error Resume Next
Exit Function
IndexError:
FnArrayHasItemAt = False
Resume ExitRoutine
End Function
Public Function FnArrayCorrectItemIndex(ByVal anArray) As Variant()
'Some arrays received from .NET comes with 0 as array size but will contain an item on the index 0
'To get the correct array item, we will remake tha array using vba
Dim a() As Variant
On Error Resume Next
Dim v As Variant
For Each v In anArray
If Not v Is Nothing Then
FnArrayAddItem a, v
End If
Next v
FnArrayCorrectItemIndex = a
End Function
Public Sub FnArrayMerge(ByRef addToArray, ByVal addFromArray)
'Merges two arrays. Arrays must be of the same kind
On Error Resume Next
If Not FnArrayHasitem(addFromArray) Then Exit Sub
Dim v As Variant
For Each v In addFromArray
FnArrayAddItem addToArray, v
Next v
End Sub
Public Function FnArrayMultiDimentioned(ByRef iArr) As Boolean
'Returns true if an array is multi dimentioned. i.e. Array(1,1) => true. Oterwise false
On Error GoTo NOT_MULTI_DIMENTIONED
Dim AW As Long
AW = UBound(iArr, 2) - 1
FnArrayMultiDimentioned = True
Exit Function
NOT_MULTI_DIMENTIONED:
FnArrayMultiDimentioned = False
End Function
Public Function FnArrayPop(ByRef byrefArray)
'Removes the last item from an array
If (Not ArrayHelper.FnArrayHasitem(byrefArray)) Then Exit Function
'Resize the array
On Error GoTo REVERSE_STATIC_ARRAY
ReDim Preserve byrefArray(UBound(byrefArray) - 1)
Exit Function
REVERSE_STATIC_ARRAY:
'Static arrays cannot be re-dimentioned.
gDll.ShowDialog "Array cannot be resized", vbCritical
End Function
Public Function FnArrayPrintValues(ByRef stringArray As Variant)
'Prints array content
Dim I As Long
For I = 0 To UBound(stringArray) - 1
DebugPrint stringArray(I)
Next I
End Function
Public Function FnArrayPush(ByRef byrefArray, ByVal iItem)
'---------------------------------------------------------------------------------------
' Procedure : FnArrayPush
' Author : KRISH
' Date : 13/03/2018
' Purpose : Adds an item to the end of an array
' Returns :
'---------------------------------------------------------------------------------------
'
'Even empty items should be added
'If IsBlank(iItem) Then Exit Function
On Error Resume Next
Dim T As Long
'Get array sieze
T = FnArrayGetSize(byrefArray)
'+1
INC T
ReDim Preserve byrefArray(0 To T)
If varType(iItem) = vbObject Then
Set byrefArray(T - 1) = iItem
Else
byrefArray(T - 1) = iItem
End If
End Function
Private Function FnArrayAddItemAt(ByRef byrefArray, ByVal Item, index As Long)
On Error Resume Next
If varType(Item) = vbObject Then
Set byrefArray(index) = Item
Else
byrefArray(index) = Item
End If
End Function
Public Function FnArrayPushToTop(ByRef byrefArray, ByVal iItem)
'---------------------------------------------------------------------------------------
' Procedure : FnArrayPushToTop
' Author : KRISH
' Date : 13/03/2018
' Purpose : Adds an item to the start of an array. Only dynamic arrays are allowed
' Returns :
'---------------------------------------------------------------------------------------
'
'Even empty item should be added
'If IsBlank(iItem) Then Exit Function
If Not IsArray(byrefArray) Then Exit Function
Dim L As Long
'Get size
L = FnArrayGetSize(byrefArray)
'Size +1
INC L
'Re allocate spaces
ReDim Preserve byrefArray(L)
'Shift all items from right
Dim I As Long
For I = (L - 1) To 1 Step -1
FnArrayAddItemAt byrefArray, byrefArray(I - 1), I
Next I
'Add the newest item to the first
FnArrayAddItemAt byrefArray, iItem, 0
'Return the array
FnArrayPushToTop = byrefArray
End Function
Public Function FnArrayRemove(ByRef sourceArray, Optional elementAt As Long = 0)
'Removes an element using its index from an array.
If ArrayIsEmpty(sourceArray) Then Exit Function
Dim L As Long
L = ArrayHelper.FnArrayGetSize(sourceArray)
If elementAt > L Then Exit Function 'Invalid element index
FnArrayRemove = sourceArray(elementAt)
If ((L - 1) = elementAt) Then 'Requested to remove the last item.
ReDim Preserve sourceArray(UBound(sourceArray, 1) - 1)
Else
'Requested to remove something in between
Dim I As Long
For I = elementAt To L - 2
sourceArray(I) = sourceArray(I + 1)
Next I
ReDim Preserve sourceArray(UBound(sourceArray, 1) - 1)
End If
FnArrayRemove = sourceArray
End Function
Public Function FnArrayReverse(ByVal byvalArray) As Variant()
'Reverses the current array. Return reversed array or null
If (Not FnArrayHasitem(byvalArray)) Then Exit Function
Dim tempArray() As Variant
Dim I As Long
Dim J As Long
Dim C As Long
On Error Resume Next
C = UBound(byvalArray)
'c cannot be 0 at this time because fnArrayHasItem returned true. => it must have at least one item
If (C = 0) Then C = 1
'Init temp array
ReDim Preserve tempArray(C)
'We are going in reverse
J = 0
For I = (C - 1) To 0 Step -1
tempArray(J) = byvalArray(I)
INC J
Next I
FnArrayReverse = tempArray
End Function
Public Function FnArrayToString(ByVal iArr, Optional delimiter As Variant = ";", Optional delimiterForNewRow As Variant = vbNewLine) As String
'Converts an array to a long delimited string
'iArr = source array
'delimiter = seperator for elements i.e. one, two,three
On Error Resume Next
Dim row As Integer
Dim Column As Integer
Dim AL As Integer 'Array length
Dim AW As Integer 'array width
If (Not ArrayHelper.FnArrayHasitem(iArr)) Then Exit Function
Select Case ArrayDimensionLength(iArr)
Case 1
FnArrayToString = VBA.Join(iArr, delimiter)
Case 2
AL = ArrayHelper.FnArrayGetSize(iArr)
If AL = 0 Then Exit Function
AW = UBound(iArr, 2)
For row = 0 To AL - 1
For Column = 0 To AW - 1
FnArrayToString = FnArrayToString & iArr(row, Column) & delimiter
Next Column
FnArrayToString = FnArrayToString & delimiterForNewRow
Next row
Case Else
End Select
EXIT_ROUTINE:
Exit Function
End Function