2016-06-23 41 views
0

我有一列數據,其中有字符串值。我想通過該列中的每個單元格進行比較,並檢查值是否重複。比較需要全文以及通配符。使用vba刪除excel中的重複行

下面

是我的數據的截圖

Screenshot

,如果你看到的截圖,該公司CES有限公司第3排的存在,以及對列17與另一家公司ECLERX SERVICES LTD沿着| CES有限公司。所以我想突出顯示這樣的重複值。

下面是我寫的

Dim rangeToUse As Range, singleArea As Range, cell1 As Range, cell2 As Range, i As Integer, j As Integer 
 

 
Set rangeToUse = Selection 
 

 
Cells.Interior.ColorIndex = 0 
 
Cells.Borders.LineStyle = xlNone 
 

 
For Each singleArea In rangeToUse.Areas 
 
    singleArea.BorderAround ColorIndex:=1, Weight:=xlThin 
 
Next singleArea 
 

 
For i = 1 To rangeToUse.Areas.Count 
 
    For Each cell1 In rangeToUse.Areas(i) 
 
    MsgBox cell1.Value 
 
     For j = 1 To rangeToUse.Areas.Count 
 
       For Each cell2 In rangeToUse.Areas(j) 
 
        If cell1.Value = cell2.Value Then 
 
         cell2.Interior.ColorIndex = 38 
 
        End If 
 
        MsgBox cell2.Value 
 
       Next cell2 
 
     Next j 
 
    Next cell1 
 
Next i

但是代碼突出顯示所有不同的細胞中的代碼。任何人都可以讓我知道我在做什麼錯?

+0

當談到創建在Excel中唯一值的列表,沒有什麼比一個很好的舊['Dictionary'對象(http://stackoverflow.com/questions/915317/does-vba - 具備詞典結構)。 – Tim

+0

它們將是相同的顏色,因爲當你遍歷列表時,在某個時候,'cell1'和'cell2'將引用同一個單元格,這當然會通過你的匹配測試。您需要排除cell1與自身進行比較。 –

回答

0

您的代碼總是會找到重複因爲你的比較之一將永遠是自己的單元格。

以下是使用Collection對象來檢測重複項的方法。如果嘗試使用與現有項目相同的密鑰添加項目,集合將返回錯誤。我們測試了這一點。

我們還需要在單元中有兩個(或更多)時拆分公司名稱。在你的示例中,它們看起來被|(被空間包圍的管道)分開,但檢查有時截圖並不理想。

看看這個讓你開始:


Option Explicit 
Sub ColorDups() 
Dim rangeToUse As Range, singleArea As Range, cell1 As Range, cell2 As Range, i As Integer, j As Integer 
Set rangeToUse = [a1:a23] 'hard coded for testing 

Cells.Interior.ColorIndex = 0 
Cells.Borders.LineStyle = xlNone 

For Each singleArea In rangeToUse.Areas 
    singleArea.ClearFormats 
    singleArea.BorderAround ColorIndex:=1, Weight:=xlThin 
Next singleArea 

'Generate Unique companies list and flag duplicates 
Dim colCompanies As Collection 
Dim vCompany As Variant 
Dim S(0 To 1) As String 
Set colCompanies = New Collection 
On Error Resume Next 
For i = 1 To rangeToUse.Areas.Count 
    For Each cell1 In rangeToUse.Areas(i) 
     vCompany = Split(cell1.Text, " | ") 
     For j = LBound(vCompany) To UBound(vCompany) 
      S(0) = Trim(vCompany(j)) 
      S(1) = cell1.Address 
      colCompanies.Add S, S(0) 
      Select Case Err.Number 
       Case 457 'we have a duplicate 
        Err.Clear 
        cell1.Interior.ColorIndex = 38 
        Range(colCompanies(S(0))(1)).Interior.ColorIndex = 38 
       Case Is <> 0 'debugstop 
        Debug.Print Err.Number, Err.Description 
        Stop 
      End Select 
     Next j 
    Next cell1 
Next i 
On Error GoTo 0 

End Sub 

這是使用你的數據和上面的宏結果。您可以通過使用多種不同的顏色和/或通過輸出匹配的單元格範圍來增強;等

enter image description here

+0

嗨@Ron,謝謝......您的解決方案幫助了我。謝謝你的幫助。我將修改爲具有不同的顏色 –

0

這會給你出現的次數在您選擇

WorksheetFunction.CountIf(rangeToUse, 「」 &小區2 & 「」)

它看來,您將可以通過迭代一個不連續的選擇。如果您希望計算小區2的區域出現的次數使用

WorksheetFunction.CountIf(rangeToUse.Areas(J), 「」 &小區2 & 「」)

0

在我看來,你正在編碼以匹配確切的單元格值,但在你的例子中,你聲明CES Limited和ECLERX SERVICES LTD | CES Limited應該返回一場比賽。

您還需要考慮如何將其標記爲不同的顏色,如果ECLERX再次出現在它自己的/其他顏色上,會發生什麼樣的顏色?

如果您確實只是想按照以下代碼返回重複項,您可能可以通過查找來實現此目的,如果您需要單獨分隔代碼公司,並且可能需要在單元格中拆分字符串,看看,如果這對你的作品,它會標誌,其中一個單元的整個字符串在列放置1構成任何其他的一部分旁邊:

Sub Whatever() 

Dim Loc As Range 
Dim Loc2 As Range 
Dim cell As Range 
Dim myrange As Range 

Set myrange = -Put Your Range Here- 

For Each cell In myrange 

    Set Loc = myrange.Cells.Find(What:=cell.Value) 
    Set Loc2 = myrange.FindNext(Loc) 
    If Not Loc2.Address = Loc.Address Then 

     Loc.Offset(0, 1) = 1 

     Do Until Loc2.Address = Loc.Address Or Loc2.Offset(0, 1) = 1 

      Loc2.Offset(0, 1) = 1 
      Set Loc2 = myrange.FindNext(Loc2) 

     Loop 

    End If 

Next cell 

Set Loc = Nothing 
Set Loc2 = Nothing 

End Sub