突出显示重复匹配下一列

时间:2017-08-01 11:11:37

标签: excel vba excel-vba conditional-formatting

想要比较当前日期和前一天的值,并逐行突出显示每个项目的重复值,我的数据有几行,我有每天从文本文件导入数据的宏。 测试数据:

enter image description here

以下是我正在尝试的代码:

Sub Duplicates()

Dim refRng As Range, cell As Range
Application.ScreenUpdating = False
With Worksheets("Sheet1")
    Set refRng = .Range("B2", .Cells(.Rows.Count, "F").End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants)
    For Each cell In .Range("C2", .Cells(.Rows.Count, "D").End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants)
        If cell.value <> 0 Then
            If Not refRng.Find(what:=cell.value, LookIn:=xlFormulas, lookat:=xlWhole, MatchCase:=False) Is Nothing Then cell.Interior.color = RGB(255, 255, 0)
        End If
    Next cell
End With
Application.ScreenUpdating = True

End Sub

3 个答案:

答案 0 :(得分:0)

对于每对should expect,每个值的出现。如果它发生多次,count它。

编辑:比较两个相邻的值,如果匹配,则将它们着色。

答案 1 :(得分:0)

您还可以使用条件格式。选择要比较的列:

条件格式&gt;突出显示单元格规则&gt;重复值

答案 2 :(得分:0)

我修改了你的代码以实现结果。以下代码将从B列开始到D列。相应地更改范围列。

Dim cell As Range

Application.ScreenUpdating = False

With Worksheets("Sheet1")
    'starts with Column C and matches C with B, D with C and so on..
    For Each cell In .Range("C2", .Cells(.Rows.Count, "D").End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants)
        'cell.Cells(1, 0) will refer previous cell
        If cell.Value = cell.Cells(1, 0) Then
            cell.Interior.Color = RGB(255, 255, 0)
            cell.Cells(1, 0).Interior.Color = RGB(255, 255, 0)
        End If
    Next cell
End With