也許你需要一個定製的VBA雙卸妝器。試試這個:
Sub RemoveVisibleDupes(r As Range, comparedCols)
Dim i As Long, j As Long, lastR As Long
i = r.Row: lastR = r.Row + r.Rows.count - 1
Do While i < lastR
For j = lastR To i + 1 Step -1
If Not (r.Rows(i).Hidden Or r.Rows(j).Hidden) And areDup(r.Rows(i), r.Rows(j), comparedCols) Then
r.Rows(j).Delete
lastR = lastR - 1
End If
Next
i = i + 1
Loop
End Sub
Function areDup(row1 As Range, row2 As Range, comparedCols) As Boolean
Dim col
For Each col In comparedCols
If row1.Cells(col).Value <> row2.Cells(col).Value Then Exit Function
Next
areDup = True
End Function
測試
Sub TestIt()
On Error GoTo Finish
Application.DisplayAlerts = False: Application.EnableEvents = False: Application.ScreenUpdating = False
' call our custom dup-remover on filtered columns A:C with comparing columns 1 and 3
RemoveVisibleDupes Sheet2.Range("A1:C" & Sheet2.Cells(Sheet2.Rows.count, 1).End(xlUp).Row), Array(1, 3)
' To use it with one column only, say 3, replace Array(1, 3) with array(3)
Finish:
Application.DisplayAlerts = True: Application.EnableEvents = True: Application.ScreenUpdating = True
End Sub
我的意思是東西有點不同:使用從你上面的例子 - 我想刪除重複的城市爲西班牙,但保留其他重複的休息的國家。 – Coco
這樣的事情:http://tinypic.com/r/nvwdcj/9 – Coco
@Coco我看到 - 我害怕這可能是一個需求...在這種情況下,這不是你的解決方案,對不起 – elmer007