我有2个工作表,第一个有40k +(Sheet1S)条目,我必须在第二个工作表(Sheet2S)中查找,它有300k条目。VBA Vlookup with Scripting Dictionary
我已经编写了这个脚本来运行基于脚本dictonary的vlookup,如果我将for循环调整为10行(我取消注释了实际的for循环),它将起作用。
Sub aTest()
Dim a As Variant, i As Long
With CreateObject("Scripting.Dictionary")
a = Sheets("Sheet2S").Range("A1").CurrentRegion.Value
For i = 2 To 10
'For i = 2 To UBound(a, 1)
.Item(a(i, 1)) = Application.Index(a, i, 0)
Next
a = Sheets("Sheet1S").Range("A1").CurrentRegion.Value
'For i = 2 To UBound(a, 1)
For i = 2 To 10
If .exists(a(i, 1)) Then
a(i, 2) = .Item(a(i, 1))(2)
'a(i, 4) = .Item(a(i, 2))(3)
Else
a(i, 2) = "#N/A"
End If
Next i
End With
Sheets("Sheet1S").Range("a1").CurrentRegion.Value = a
End Sub
现在根据一个古老的线程(How to optimize vlookup for high search count ? (alternatives to VLOOKUP))字典方法应该只有几秒钟:不过,如果我使用所有的行它需要年龄。如果我使用Application.Vlookup,则需要10分钟才能完成对我来说太长的完全相同的工作表。我正在使用Excel 2016,并添加了Microsoft脚本运行时。难道我做错了什么?
在此先感谢。
最佳
你实现这个方法看似寻常给我。多少列?你在这两个列表中有没有重复的地方? –
两张表都包含15列,可能有重复。然而,随着保罗比卡的实施,它就像一个魅力。 – Uwewewe