2017-03-03 87 views
0

當前有一個名爲「Scan for Unassociated CC」的按鈕的Excel文檔。查找並突出顯示重複項目

當我點擊該按鈕下面的模塊揭開序幕:

Sub scan() 
Dim dataRange As Range 
Dim oneCell As Range 

With ThisWorkbook.Sheets("Resource Info").Range("C:C"): Rem adjust 
    Set dataRange = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)) 
End With 

For Each oneCell In dataRange 
    If 1 < Application.CountIf(dataRange, oneCell.Value) Then 
     With oneCell 
      .EntireRow.Interior.ColorIndex = 6 
     End With 
    End If 
Next oneCell 
End Sub 

這凸顯了C列中

一個重複的值添加到該模塊,什麼是最好的辦法都行只需突出顯示列C和列K中具有重複值的行?

我是VBA和學習的新手。任何幫助是極大的讚賞。謝謝!

+1

將dataRange2設置爲K列,並使用另一個COUNTIF並將它們相加。 –

+1

由於你是VBA的新手:** A)**'With'語句末尾沒有''''的原因 - 這是命令分隔符,並且在該行沒有另一個可執行語句。 ** B)**不要使用'REM'作爲評論 - 它已被棄用,並且使得_look_像一個n00b;)使用單引號'''代替。 – FreeMan

+0

感謝提示@FreeMan! – Alex

回答

3
Sub scan() 
    Dim dataRange As Range 
    Dim dataRange2 As Range 
    Dim oneCell As Range 
    Dim oneCell2 As Range 
    Dim WS As Worksheet 

    Set WS = ThisWorkbook.Sheets("Resource Info") 
    WS.Cells.Interior.Color = -4142 


    With WS.Range("C:C"): Rem adjust 
     Set dataRange = Range(.Cells(1, 1), .Cells(.Rows.count, 1).End(xlUp)) 
    End With 

    Set dataRange2 = dataRange.Offset(0, 8) 

    For Each oneCell In dataRange 
     Set oneCell2 = oneCell.Offset(0, 8) 
     If 1 < Application.WorksheetFunction.CountIfs(dataRange, oneCell, dataRange2, oneCell2) Then 
      With oneCell 
       .EntireRow.Interior.ColorIndex = 6 
      End With 
     End If 
    Next oneCell 
End Sub 
+0

謝謝@lbo - 非常感謝細節! – Alex