如果单元格与相邻单元格匹配,如何突出显示单元格

How to highlight cells if they match neighboring cells

我经常处理跨多列的数据,需要一种方便的方法来突出显示特定列中包含相同值的多行,但我需要在突出显示和非突出显示之间切换。

例如,我将在 A 列中有几行数据,例如: 700105862 700105862 700105862 700103235 700103235 700108783 700108783 700108783

我想要做的是突出显示前三行 (700105862),然后不突出显示 700103235,然后再次突出显示 700108783。 我想知道是否有一个条件格式公式可以使这成为可能。

如有任何帮助,我们将不胜感激!

谢谢,

当然,如果您知道要突出显示的范围,只需将条件格式设置为介于 x 和 y 值之间即可。用你没有得到的评论这个问题,我会相应地修改答案。

如果您的数字被分成 总是 不同数字重复的块,那么您可以使用此 VBA 代码:

Sub main()
    Dim item As Variant
    Dim startRow As Long
    Dim okHighlight As Boolean

    With Range("A1", Cells(Rows.count, 1).End(xlUp))
        For Each item In GetUniqueValues(.Cells).Items
            If okHighlight Then .Range(.Cells(startRow, 1), .Cells(item, 1)).Interior.ColorIndex = 48
            startRow = item + 1
            okHighlight = Not okHighlight
        Next
    End With
End Sub


Function GetUniqueValues(rng As Range) As Dictionary
    Dim cell As Range
    Dim dict As Dictionary

    Set dict = New Dictionary
    With dict
        For Each cell In rng
            .item(cell.Value) = cell.row - rng.Rows(1).row + 1
        Next
    End With
    Set GetUniqueValues = dict
End Function

a 条件格式 方法可以使用 helper

假设:

  • 您的数据在 A 列,从第 2 行开始

  • B 列空闲

然后:

  • helper B 列单元格中写入以下公式:

    =IF(A2<>A1,B1+1,0)

  • 使用以下公式对 A 列应用条件格式:

    =INT(B2/2)=B2/2

    并选择您喜欢的格式来突出显示单元格