我是VBA脚本的新手......我想做的是:
-
检查MS Excel文件中重复值的列
-
如果存在重复项,则将包含重复值的行复制到另一个工作表...
例如,我有一个内容为sheet1:
我想浏览A列中的内容,并将包含A列中重复值的行导出到新工作表:
在搜索和编辑一些VBA脚本后,我想出了这段代码:
Sub FilterAndCopy()
Dim wstSource As Worksheet, _
wstOutput As Worksheet
Dim rngCell As Range, _
rngMyData As Range
Dim lngMyRow As Long
Set wstSource = Worksheets("Sheet1")
Set wstOutput = Worksheets("Sheet2")
Set rngMyData = wstSource.Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Application.ScreenUpdating = False
For Each rngCell In rngMyData
If Evaluate("COUNTIF(" & rngMyData.Address & "," & rngCell.Address & ")") > 1 Then
lngMyRow = wstOutput.Cells(Rows.Count, "A").End(xlUp).Row + 1
wstSource.Range("A" & rngCell.Row & ":D" & rngCell.Row).Copy _
Destination:=wstOutput.Range("A" & lngMyRow & ":D" & lngMyRow)
End If
Next rngCell
Application.ScreenUpdating = True
End Sub
Is this correct code? can it be optimized to be faster?
我有80.000条记录可以通过它...
2 回答
edit :添加了另一个替代代码(参见"2nd code"),它应该更快,更快
尝试这些优化
第一个代码:
“第二代码”
有很多方法可以做到这一点 . 为了使它更简单,我尝试仅改变你的循环 . PFB更改的代码 -