2017-01-24 46 views
0

我正在尋找一種快速方法來刪除特定列中的重複項,但僅限於過濾範圍內。所以,基本上我希望它只刪除可見的重複值,但留下「未過濾和隱藏」的其餘部分。VBA - 刪除過濾列中的重複項

我有這樣的一段代碼,而且不知道如何改變它這樣做:

ActiveSheet.Range("A:ZZ").RemoveDuplicates Columns:=Array(3), Header:=xlYes 

能否請你幫忙嗎?有沒有簡單的方法來編輯現有的代碼來做到這一點?

*例如:

  • 柱A =洲
  • 列B =國家
  • C欄=城市

如果我由印度過濾國家(列B)予多次重複看到多個城市(col C)。我想刪除重複項目並只查看每個城市的其中一個。不過,我不希望重複,爲其他國家被刪除。*

回答

1

您可以刪除重複所有的大陸國家,城市組合沒有在你的RemoveDuplicates參數指定了所有3過濾。這不是完全回答你的問題,但它可能是你需要的解決方案,只需少一步。

對於具有列A,B和C作爲洲,國家和城市的例子,如何執行以下操作:

ActiveSheet.Range("A:C").RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes 

注意Array部指定從範圍,將列1和3進行評估,它將在所有3列中查找重複內容(而不僅僅是現有代碼中的第3列)。

因爲宏不允許「撤消」,所以我建議在數據副本上進行測試。

下面是一個示例的屏幕截圖。原始列表在右側,結果列表在左側(在列A-C中)。注意:「倫敦」和「伯明翰」:

enter image description here

+0

我的意思是東西有點不同:使用從你上面的例子 - 我想刪除重複的城市爲西班牙,但保留其他重複的休息的國家。 – Coco

+0

這樣的事情:http://tinypic.com/r/nvwdcj/9 – Coco

+0

@Coco我看到 - 我害怕這可能是一個需求...在這種情況下,這不是你的解決方案,對不起 – elmer007

0

您可能是Range對象的SpecialCells(xlCellTypeVisible)財產之後。所以,你的代碼可能是:

ActiveSheet.Range("A:ZZ").SpecialCells(xlCellTypeVisible).RemoveDuplicates Columns:=Array(3), Header:=xlYes 

它留下的空行,不過,一旦你刪除的過濾器。我知道的唯一另外一種方式(不留空行)是使用自己的重複查找例程刪除重複項。 SpecialCells屬性仍可用於僅檢查過濾的數據。這樣的事情:

Dim uniques As Collection 
Dim cell As Range, del As Range 
Dim exists As Boolean 
Dim key As String 

Set uniques = New Collection 
For Each cell In ActiveSheet.Range("A:ZZ").Columns(3).SpecialCells(xlCellTypeVisible).Cells 
    key = CStr(cell.Value2) 
    exists = False 
    On Error Resume Next 
    exists = uniques(key) 
    On Error GoTo 0 
    If Not exists Then 
     uniques.Add True, key 
    Else 
     If del Is Nothing Then 
      Set del = cell 
     Else 
      Set del = Union(del, cell) 
     End If 
    End If 
Next 
If Not del Is Nothing Then 
    del.EntireRow.Delete 
End If 
0

也許你需要一個定製的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