I encountered the same issue several days ago while building a financial report. I would like to say that the discussion here allowed me to borrow some ideas. However, I devised my own algorithm. I would like to share it with the community. I provided the body of the algo with brief notes followed by extended explanation of the notes. I hope somebody will give my code wider application.
Sub Deleting_Duplicates_From_Array()
Dim arrayAR() As Variant
Dim intRowsAR As Integer
Dim arrayInterim() As Variant
Dim k As Integer
' Comment line 0
ReDim arrayAR(1 To intRowsAR)
k = 1
ReDim arrayInterim(1 To 1)
Dim i As Integer
Dim j As Integer
' Comment line 1
For i = LBound(arrayAR) To UBound(arrayAR)
For j = LBound(arrayAR) To UBound(arrayAR)
' Comment line 2
If i <> j And arrayAR(i, 1) = arrayAR(j, 1) And arrayAR(i, 1) <> "" And arrayAR(i, 1) <> " " Then
arrayAR(j, 1) = ""
End If
Next j
Next i
' Comment line 3
For i = LBound(arrayAR) To UBound(arrayAR)
If arrayAR(i, 1) <> "" Then
arrayInterim(k) = arrayAR(i, 1)
' Comment line 4
k = k + 1
ReDim Preserve arrayInterim(1 To k)
End If
' Comment line 5
If i = UBound(arrayAR) Then
k = k - 1
ReDim Preserve arrayInterim(1 To k)
End If
Next i
' Comment line 6
k = UBound(arrayInterim)
' Comment line 7
ReDim arrayAR(1 To k)
' Commen line 8
arrayAR = arrayInterim
End Sub
Variable Definition
arrayAR - an array for contracts from a general ledger.
intRowsAR - a variable for the number of the contracts from the general ledger
arrayInterim - a temporary array for storing unique values found in arrayAR
k - a counter variable for determing dimension of arrayInterim
i - a counter variable for launching one cycle through the elements of arrayAR
j - a counter variable for launching another cycle through the elements of arrayAR
Comment Lines
Comment line 0: Define dimensions of arrayAR and arrayInterim
Comment line 1: Launch the cycles I and J, find duplicate values in arrayAR, replace them with an empty value
Comment line 2: Examine similarity of all the elements of arrayAR pairwise, examine absence of empty values and space values among the elements of the initial version of arrayAR
Comment line 3: Launch the cycle I once again and put the unique values from arrayAR in arrayInterim
Comment line 4: Change dimension of arrayInterim
Comment line 5: Detect the last iteration in the cycle i and delete the last empty element of arrayInterim created once and previously at the last iteration
Comment line 6: Put a figure for the dimension of arrayInterim into the variable K
Comment line 7: Define the new dimensiom of arrayAR without saving the initial elements of arrayAR
Comment line 8: Put the unique values of the initial arrayAR from arrayInterim into the newly-dimensioned arrayAR