【发布时间】:2016-02-12 13:56:00
【问题描述】:
我正在处理一个相当大的数据集(>100,000 行)并尝试比较两个列表以找出新列表中的哪些项目尚未在主列表中。换句话说,我想找到新的独特物品。
我有一些使用 vlookup 的 VBA 代码和有效的数组,但是当数组变得太大(~70,000)时会爆炸。所以我转向收藏。但是,我在使用 vlookup 或 match 搜索集合时遇到了困难。
Sub find_uniqueIDs()
Dim a As Long
Dim n As Long
Dim m As Variant
Dim oldnum As Long
Dim oldIDs As Variant
Dim oldcoll As New Collection
Dim newnum As Long
Dim newIDs As Variant
Dim newcoll As New Collection
oldnum = 75000
oldIDs = Range("A1", Range("A" & oldnum))
newnum = 45000 + 3
newIDs = Range("G3", Range("G" & newnum))
'Using arrays to search, but bombs out when oldnum or newnum are ~70000
For n = 1 To newnum - 3
m = Application.VLookup(newIDs(n, 1), oldIDs, 1, False)
If IsError(m) Then Range("E100000").End(xlUp).Offset(1, 0) = newIDs(n, 1)
Next n
'Using collections to search
For n = 1 To oldnum
On Error Resume Next
oldcoll.Add oldIDs(n, 1)
On Error GoTo 0
Next n
For m = 1 To newnum
On Error Resume Next
newcoll.Add newIDs(m, 1)
On Error GoTo 0
Next m
'This bit of code doesn't work
For a = 1 To newcoll.Count
If Application.VLookup(newcoll(a), oldcoll, 1, False) = "#N/A" Then _
Range("E100000").End(xlUp).Offset(1, 0) = newcoll(a)
Next a
End Sub
任何想法如何使用集合确定特定项目是否在主列表中?
【问题讨论】:
-
您是否考虑过 Scripting.Dictionary 对象在 key 上有自己的主唯一索引?另外,如果你只想证明存在,为什么不用
Application.Match而不是 VLOOKUP? -
脚本字典确实可能是最好的解决方案。对于公式,'COUNTIF()' 比 'VLOOKUP()' 快无数倍
-
在构建集合时使用键:stackoverflow.com/a/21095201/4604845
-
@iDevlop - MATCH function 更快(参见Is there a faster CountIF)
-
那么使用 SQL 查询您的 Excel 工作表呢? stackoverflow.com/a/8766541/78522