突出显示单个范围中的重复项

时间:2016-03-27 13:55:30

标签: excel vba excel-vba duplicates conditional-formatting

假设范围是连续的&是一栏。

我想突出显示上述RANGE的重复条目。我的VBA如下,但没有按预期运行。主题是使用OFFSET

比较第一个单元格值与其底部值
C:\Windows\system32\cmd.exe /C mingw32-make -e -f  Makefile
"mingw32-make" ­¥ ï¥âáï ¢­ãâ७­¥© ¨«¨ ¢­¥è­¥©
ª®¬ ­¤®©, ¨á¯®«­ï¥¬®© ¯à®£à ¬¬®© ¨«¨ ¯ ª¥â­ë¬ ä ©«®¬.
====0 errors, 0 warnings====

3 个答案:

答案 0 :(得分:2)

尝试条件格式规则。如果它看起来更容易编码。

With Worksheets("Sheet1")
    With .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp))
        .FormatConditions.Delete
        .FormatConditions.AddUniqueValues
        With .FormatConditions(1)
            .Interior.Color = vbGreen
        End With
    End With
End With

cf_duplicates

答案 1 :(得分:1)

您只需要单循环

Sub CompareSingleRange()
    Dim rangeToUse1 As Range, cell1 As Range
    Dim wf As WorksheetFunction
    Set wf = Application.WorksheetFunction
    ' Assume Selection is contiguous
    Set rangeToUse1 = Selection

        For Each cell1 In rangeToUse1
                If wf.CountIf(rangeToUse1, cell1) > 1 Then
                    cell1.Interior.ColorIndex = 38
                End If
        Next cell1
End Sub

enter image description here

答案 2 :(得分:1)

您可以使用Excel Worksheet Functions来完成此任务;否则,"纯" VBA解决方案如下所示(您只需稍微修改原始Sub并附加条件):

Sub FindDuplicates()
    Dim rangeToUse1 As Range, cell1 As Range, cell2 As Range
    Set rangeToUse1 = Selection
    For Each cell1 In rangeToUse1
        For Each cell2 In rangeToUse1
           If cell1.Value = cell2.Value And cell1.Row <> cell2.Row Then
              cell1.Interior.ColorIndex = 38
           End If
        Next cell2
    Next cell1
End Sub

希望这会有所帮助。