com*_*ski 1 excel vba excel-vba excel-formula excel-2016
这是一个跟进的问题,如何删除Excel中的情况敏感的重复项(对于100k或更多记录)? .
由于他的代码过程只处理A 列的数据,如果找到区分大小写的副本,我还想删除整行数据.
区分大小写:
- 情况1
- 情况1
- 情况1
都是独特的记录.
您可以使用a Dictionary检查二进制唯一性和变量数组以加快速度.要使用字典,您需要包含对Microsoft Scripting Runtime Library的引用
(工具>参考> Microsoft Scripting Runtime库)
我用100,000行测试了这个,平均0.25秒在我的笔记本电脑上.
Sub RemoveDuplicateRows()
Dim data As Range
Set data = ThisWorkbook.Worksheets("Sheet1").UsedRange
Dim v As Variant, tags As Variant
v = data
ReDim tags(1 To UBound(v), 1 To 1)
tags(1, 1) = 0 'keep the header
Dim dict As Dictionary
Set dict = New Dictionary
dict.CompareMode = BinaryCompare
Dim i As Long
For i = LBound(v, 1) To UBound(v, 1)
With dict
If Not .Exists(v(i, 1)) Then 'v(i,1) comparing the values in the first column
tags(i, 1) = i
.Add Key:=v(i, 1), Item:=vbNullString
End If
End With
Next i
Dim rngTags As Range
Set rngTags = data.Columns(data.Columns.count + 1)
rngTags.Value = tags
Union(data, rngTags).Sort key1:=rngTags, Orientation:=xlTopToBottom, Header:=xlYes
Dim count As Long
count = rngTags.End(xlDown).Row
rngTags.EntireColumn.Delete
data.Resize(UBound(v, 1) - count + 1).Offset(count).EntireRow.Delete
End Sub
Run Code Online (Sandbox Code Playgroud)
基于这个问题的精彩答案