编辑的方法:而不是为我的解决方案,使用类似比较快2列
For i = 1 To tmpRngSrcMax
If rngSrc(i) <> rngDes(i) Then ...
Next i
这大约要快100倍。
我必须使用VBA比较包含字符串数据的两列。这是我的方法:
Set rngDes = wsDes.Range("A2:A" & wsDes.Cells(Rows.Count, 1).End(xlUp).Row)
Set rngSrc = wsSrc.Range("I3:I" & wsSrc.Cells(Rows.Count, 1).End(xlUp).Row)
tmpRngSrcMax = wsSrc.Cells(Rows.Count, 1).End(xlUp).Row
cntNewItems = 0
For Each x In rngSrc
tmpFound = Application.WorksheetFunction.CountIf(rngDes, x.Row)
Application.StatusBar = "Processed: " & x.Row & " of " & tmpRngSrcMax & "/" & Format(x.Row/tmpRngSrcMax, "Percent")
DoEvents ' keeps Excel away from the "Not responding" state
If tmpFound = 0 Then ' new item
cntNewItems = cntNewItems + 1
tmpLastRow = wsDes.Cells(Rows.Count, 1).End(xlUp).Row + 1 ' first empty row on target sheet
wsDes.Cells(tmpLastRow, 1) = wsSrc.Cells(x.Row, 9)
End If
Next x
所以,我用一个For Each循环来遍历槽1日(SRC)柱,和COUNTIF方法来检查,如果该项目是在第二个已经存在(DES)柱。如果不是,则复制到第一个(src)列的末尾。
该代码可以工作,但在我的机器上,大约需要7000行的列需要200秒。我注意到当直接用作公式时,CountIf的工作方式更快。
有没有人有代码优化的想法?
你可以使用一个O(n)的算法,如果对数据进行排序。这将是我的优化方法。 – Bathsheba
忘记使用Worksheetfucntion来处理如此庞大的数据。将数据复制到数组,然后进行比较。你会惊喜于速度;) –
'ScreenUpdating = false'在你的代码开始处也会有所帮助。 –