查找并突出显示重复项

时间:2017-03-03 21:24:05

标签: excel vba excel-vba

目前有一个带有名为&#34的按钮的Excel文档;扫描未关联的CC"。

当我点击按钮时,以下模块启动:

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 个答案:

答案 0 :(得分: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