我需要微調我的代碼以刪除除最後發生的所有重複項。重複將由多列(列A,B,C)定義。下一列總是有不同的數字,因此在定義重複項時會被忽略。我需要刪除整行。在單元格比較旁邊過濾和執行單元格也不起作用,因爲那樣它就不知道最後一次發生哪一個單元。 See example table刪除重複的行,除了最後一次發生 - VBA Excel 2013
Sub DuplicateDelete()
Dim Rng As Range
Dim Dn As Range
Dim nRng As Range
Dim Q As Variant
Dim K As Variant
With Sheets("Sheet1")
Set Rng = .Range(.Range("A9:C9"), .Range("A" & Rows.Count).End(xlUp))
End With
On Error Resume Next
Rng.SpecialCells(xlCellTypeBlanks).Resize(, Columns.Count).Delete
On Error GoTo 0
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Dn <> "" Then
If Not .Exists(Dn.Value) Then
.Add Dn.Value, Array(Nothing, Dn)
Else
Q = .Item(Dn.Value)
If nRng Is Nothing Then
Set Q(0) = Q(1)
Set Q(1) = Dn
Set nRng = Q(0)
Else
Set Q(0) = Q(1)
Set nRng = Union(nRng, Q(0))
Set Q(1) = Dn
End If
.Item(Dn.Value) = Q
End If
End If
Next
If Not nRng Is Nothing Then
nRng.EntireRow.Delete
End If
End With
End Sub
例如在上述列表中,我需要在上面兩行被刪除,因爲它們是重複的最後2行。我只需要一臺能夠掃描所有行的掃描儀,並決定刪除除最後一次出現以外的所有重複項。有可能嗎?提前感謝您的幫助。
*注意:這不是我自己的代碼,我是VBA的初學者級別,它是從搜索引用的。
您可以添加您試圖「微調」的代碼。 – Ambie
在上面添加。 – Marshall
表中最後一次使用的列是什麼? – 2016-12-03 11:11:26