NARAYANK991
Excel Ninja
Hi SirJB7 ,
I tried the following code on the data in your file ; I do not know about the timing , but I think the code is more readable ?
[pre]
[/pre]
This is from : http://fastexcel.wordpress.com/2012/07/10/comparing-two-lists-vba-udf-shootout-between-linear-search-binary-search-collection-and-dictionary/
As mentioned in the link :
Because the function uses the Dictionary Object from VBScript you need to add a reference to the Microsoft Scripting Runtime.
Narayan
I tried the following code on the data in your file ; I do not know about the timing , but I think the code is more readable ?
[pre]
Code:
Public Sub Compare_Lists()
With Application
.EnableEvents = False
.Calculation = xlManual
End With
Dim dictFor As New Dictionary, dictIn As New Dictionary
Dim nLookFor As Long, nLookIn As Long, nLookMax As Long
Dim lrow_colA As Long, lrow_colC As Long
Dim I As Long, j As Long, k As Long
Dim newvals As Variant
lrow_colA = Cells(Rows.Count, 1).End(xlUp).Row
lrow_colC = Cells(Rows.Count, 3).End(xlUp).Row
LookFor = Range("C2:C" & lrow_colC).Value2
LookIn = Range("A2:A" & lrow_colA).Value2
newvals = LookFor
nLookFor = UBound(LookFor)
nLookIn = UBound(LookIn)
nLookMax = Application.Max(nLookFor, nLookIn)
On Error Resume Next
For j = 1 To nLookIn
dictIn.Add LookIn(j, 1), LookIn(j, 1) ' dictionary
Next j
For j = 1 To nLookFor
dictFor.Add LookFor(j, 1), LookFor(j, 1) ' dictionary
Next j
On Error GoTo 0
I = 1
k = 1
For j = 1 To nLookFor
If Not (dictIn.Exists(LookFor(j, 1))) Then
newvals(k, 1) = LookFor(j, 1)
k = k + 1
End If
Next
Range("E2:E" & nLookMax + 1).ClearContents
Range("E2:E" & k).Value = newvals
I = 1
k = 1
For j = 1 To nLookIn
If Not (dictFor.Exists(LookIn(j, 1))) Then
newvals(k, 1) = LookIn(j, 1)
k = k + 1
End If
Next
Range("G2:G" & nLookMax).ClearContents
Range("G2:G" & k).Value = newvals
With Application
.EnableEvents = True
.Calculation = xlAutomatic
End With
Set newvals = Nothing
End Sub
This is from : http://fastexcel.wordpress.com/2012/07/10/comparing-two-lists-vba-udf-shootout-between-linear-search-binary-search-collection-and-dictionary/
As mentioned in the link :
Because the function uses the Dictionary Object from VBScript you need to add a reference to the Microsoft Scripting Runtime.
Narayan