如何使用 FindFirst 函数比较 2 个内存记录集?
How to use FindFirst Function to compare 2 in-memory record-set?
我想遍历 A 列并检查 B 列中是否存在任何值。我目前正在使用 .Find 函数,但是当我开始处理大量行(>60 000)时,它开始花了很长时间 运行 代码。
我想我可以在内存中为每一列创建 2 个记录集并使用 .FindFirst 比较它们,但我无法让它工作。我认为这是因为我没有使用任何 "ADO/DAO" 连接,因为我的数据在工作簿本身中。
有没有办法在 B 列中为 A 列的每个值快速找到匹配项?
我尝试将代码更改为 .FindFirst 并使用记录集,但它一直显示 "The Object does not support property etc..."。
For Each cel In rngRD.Cells
With ThisWorkbook.Sheets("RawData").Range("A1:A" & Last_Row_DB)
.Cells(1, 1).Activate
Set CRef = .Find(What:=cel, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If cannot be found then
If CRef Is Nothing Then
'Do Something
Else
Set CRef = .FindNext(CRef)
End If
End With
Next cel
我无法让它与字典一起工作,但找到了另一种方法来完成我需要的事情,并且对于行数 >60 000 的计算时间非常快。我现在能做的最好了!
Sub compareData()
Dim ListA As Range
Dim ListB As Range
Dim c As Range
'Create recordset to hold values to copy
Set rs = New Recordset
With rs
.Fields.Append "ID", adVarChar, 1000, adFldIsNullable
.Fields.Append "Sector", adVarChar, 1000, adFldIsNullable
.Fields.Append "Category", adVarChar, 1000, adFldIsNullable
.Fields.Append "Description", adVarChar, 1000, adFldIsNullable
.Fields.Append "DayNum", adVarChar, 1000, adFldIsNullable
.Fields.Append "Site", adVarChar, 1000, adFldIsNullable
.Fields.Append "Prod", adVarChar, 1000, adFldIsNullable
.Fields.Append "SU", adInteger, , adFldMayBeNull
.Fields.Append "BaseUnit", adInteger, , adFldMayBeNull
.Open
End With
'Define 2 lists to compare (ID's)
ListARange = Sheets("DATA").Cells(Rows.Count, "A").End(xlUp).Row 'Find the last row with data on column A
ListBRange = Sheets("RAW DATA").Cells(Rows.Count, "A").End(xlUp).Row 'Find the last row with data on column B
Set ListA = Sheets("DATA").Range("A2:A" & ListARange) 'Set your range only until the last row with data
Set ListB = Sheets("RAW DATA").Range("A2:A" & ListBRange)
'Check if ID already exists in the list, if not, add to recordSet
For Each c In ListB
If Application.CountIf(ListA, c) = 0 Then
rs.AddNew
rs!ID = c
rs!Sector = c.Offset(0, 1)
rs!Category = c.Offset(0, 2)
rs!Description = c.Offset(0, 3)
rs!DayNum = c.Offset(0, 4)
rs!Site = c.Offset(0, 5)
rs!Prod = c.Offset(0, 6)
rs!SU = c.Offset(0, 7)
rs!BaseUnit = c.Offset(0, 8)
rs.Update
End If
Next c
我想遍历 A 列并检查 B 列中是否存在任何值。我目前正在使用 .Find 函数,但是当我开始处理大量行(>60 000)时,它开始花了很长时间 运行 代码。
我想我可以在内存中为每一列创建 2 个记录集并使用 .FindFirst 比较它们,但我无法让它工作。我认为这是因为我没有使用任何 "ADO/DAO" 连接,因为我的数据在工作簿本身中。
有没有办法在 B 列中为 A 列的每个值快速找到匹配项?
我尝试将代码更改为 .FindFirst 并使用记录集,但它一直显示 "The Object does not support property etc..."。
For Each cel In rngRD.Cells
With ThisWorkbook.Sheets("RawData").Range("A1:A" & Last_Row_DB)
.Cells(1, 1).Activate
Set CRef = .Find(What:=cel, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If cannot be found then
If CRef Is Nothing Then
'Do Something
Else
Set CRef = .FindNext(CRef)
End If
End With
Next cel
我无法让它与字典一起工作,但找到了另一种方法来完成我需要的事情,并且对于行数 >60 000 的计算时间非常快。我现在能做的最好了!
Sub compareData()
Dim ListA As Range
Dim ListB As Range
Dim c As Range
'Create recordset to hold values to copy
Set rs = New Recordset
With rs
.Fields.Append "ID", adVarChar, 1000, adFldIsNullable
.Fields.Append "Sector", adVarChar, 1000, adFldIsNullable
.Fields.Append "Category", adVarChar, 1000, adFldIsNullable
.Fields.Append "Description", adVarChar, 1000, adFldIsNullable
.Fields.Append "DayNum", adVarChar, 1000, adFldIsNullable
.Fields.Append "Site", adVarChar, 1000, adFldIsNullable
.Fields.Append "Prod", adVarChar, 1000, adFldIsNullable
.Fields.Append "SU", adInteger, , adFldMayBeNull
.Fields.Append "BaseUnit", adInteger, , adFldMayBeNull
.Open
End With
'Define 2 lists to compare (ID's)
ListARange = Sheets("DATA").Cells(Rows.Count, "A").End(xlUp).Row 'Find the last row with data on column A
ListBRange = Sheets("RAW DATA").Cells(Rows.Count, "A").End(xlUp).Row 'Find the last row with data on column B
Set ListA = Sheets("DATA").Range("A2:A" & ListARange) 'Set your range only until the last row with data
Set ListB = Sheets("RAW DATA").Range("A2:A" & ListBRange)
'Check if ID already exists in the list, if not, add to recordSet
For Each c In ListB
If Application.CountIf(ListA, c) = 0 Then
rs.AddNew
rs!ID = c
rs!Sector = c.Offset(0, 1)
rs!Category = c.Offset(0, 2)
rs!Description = c.Offset(0, 3)
rs!DayNum = c.Offset(0, 4)
rs!Site = c.Offset(0, 5)
rs!Prod = c.Offset(0, 6)
rs!SU = c.Offset(0, 7)
rs!BaseUnit = c.Offset(0, 8)
rs.Update
End If
Next c