0
我期待刪除包含40-50,000行的數據集中的重複項(保留空白)。 我現在的代碼將保留第一個和最後一個實例,但我只需要保留第一個,而刪除其餘的。刪除重複但保留第一個實例VBA宏
Sub dltedups()
Dim toDelete As Range: Set toDelete = Sheet1.Rows(999999) '(to not start with
a null range)
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim a As Range
For Each a In Sheet1.Range("A7", Sheet1.Range("A999999").End(xlUp))
If Not dict.Exists(a.Value2) Then
dict(a.Value2) = 0
Else
If dict(a.Value2) = 1 Then Set toDelete = Union(toDelete,
Sheet1.Rows(dict(a.Value2)))
dict(a.Value2) = a.Row
End If
Next
toDelete.Delete
End Sub
我得到一個運行時錯誤438 ..也將這是有效的大型數據集50,000+? – Tom
請參閱編輯,是的它會比循環更快。 –
一切都很好,雖然它也刪除了空白。有沒有辦法消除這種情況? – Tom