I have struggled with the algorithm comparing 2 arrays as well. I borrowed some ideas from the discussion here. Ultimately, I developed my own algorithm. I would like to share it with the community. I made some notes in the body of the algorithm. Further, I provided extended explanation. I hope somebody will find my way of thinking helpful.
Sub Comparing_2_Arrays()
Dim arrayOSV6001() As Variant
Dim arrayPayTerms() As Variant
Dim arrayContracts() As Variant
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim intRowsOSV6001 As Integer
Dim intRowsPayTerms As Integer
Dim m As Variant
Dim n As Variant
' Comment line 0:
l = 1
k = 1
' Comment line 1
ReDim arrayOSV6001(1 To intRowsOSV6001)
ReDim arrayPayTerms(1 To intRowsPayTerms)
ReDim arrayContracts(1 To 1)
' Comment line 2
For i = 1 To UBound(arrayOSV6001)
' Comment line 3
For j = 1 To UBound(arrayPayTerms)
m = arrayOSV6001(i, 1)
n = arrayPayTerms(j, 1)
' Comment line 4
If m <> n Then
k = k + 1
Else
k = k + 0
End If
Next j
' Comment line 5
If k = j Then
arrayContracts(l) = m
' Comment line 6
k = 1
l = l + 1
ReDim Preserve arrayContracts(1 To l)
Else
k = 1
End If
Next i
' Comment line 7
If l = 1 Then
MsgBox "Missing and unique elements haven't been found."
Exit Sub
Else
' Comment line 8
l = l - 1
ReDim Preserve arrayContracts(1 To l)
End If
End Sub
Variable definition
arrayOSV6001 - array for contracts from general ledger
arrayPayTerms - array for contracts from payment terms register
arrayContracts - array for unique contracts missing in arrayPayTerms, but existing in arrayOSV6001
i - counter variable for elements of array of arrayOSV6001
j - counter variable for elements of array of arrayPayTerms
k - counter variable for the number of the elements of arrayPayTerms that are NOT equal to the elements of arrayOSV6001
l - variable for determining dimension of arrayContracts
intRowsOSV6001 - variable for determining dimension of arrayOSV6001
intRowsPayTerms - variable for determining dimension of arrayPayTerms
m -variable for values of the elements of arrayOSV6001
n - variable for values of the elements of arrayPayTerms
Comment lines
Comment line 0: Define initial values of the variables: l and k
Comment line 1: Define initial dimensions of the arrays: arrayOSV6001, arrayPayTerms, arrayContracts.
intRowsOSV6001 equals to the number of contracts downloaded from the general ledger.
intRowsPayTerms equals to the number of contracts downloaded from the payment terms register.
Comment line 2: Launch the cycle to examine the values of the elements of arrayOSV6001
Comment line 3: Launch the cycle to examine the values of the elements of arrayPayTerms
Comment line 4: Compare the values of arrayOSV6001 and arrayPayTerms in couples, then increase the K counter by one if the values are NOT similar.
Comment line 5: If the number of the elements of arrayPayTerms that are NOT equal to an element of arrayOSV6001equals to the number of iterations in the J cycle, it implies that a unique element of arrayOSV6001 has been found. Then record the found unique element in arrayContracts.
Comment line 6: Decrease the K counter to 0 and increaze the dimension of arrayContracts by 1 for the next unique element.
Comment line 7: If the dimension of arrayContracts hasn't changed after the L and J cycles have finished, it means that the unique elements missing in arrayPayTerms and existing in arrayOSV6001 haven't been found. Then exit from the sub procedure.
Comment line 8: If the dimension of arrayContracts has changed after the L and J cycles have finished, then redimension arrayContracts and eliminate the last empty element from arrayContracts that have been created for the next unique element of arrayOSV6001