突出显示连续 3 次值相同的单元格

Highlight cells where the value is the same 3 consecutive times

我试图突出显示 H 列中值连续 3 次相同的单元格,例如

  1. 一个
  2. 一个
  3. N
  4. P
  5. P
  6. P
  7. N
  8. B
  9. B

3 个 P 将以红色突出显示,但 2 个 A 或 2B 不会。这在 VBA 中是否可行,或者使用我不知道的公式,但我已经尝试了一段时间。我确定它可能很简单,我只是陷入了一个循环

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

只要您的数据开始位置上方有几行...

条件格式有点棘手。您可以 select H3 并尝试这个公式:

= OR( AND(H3=H1, H3=H2), AND(H3=H2, H3=H4), AND(H3=H4, H3=H5) )

然后将 "Applies to" 更改为整个范围。

更新

= OR( AND(H1="P",H2="P",H3="P"), AND(H2="P",H3="P",H4="P"), AND(H3="P",H4="P",H5="P") )

我没有测试太多的快速代码,但试试这个:

    Sub Find_ThreeInARow()
    Dim column1 As Range
    Dim x As Range
    Dim y As Range
    Dim z As Range

    Set column1 = Application.InputBox("Select Column to Evaluate", Type:=8)
    If column1.Columns.Count > 1 Then
        Do Until column1.Columns.Count = 1
            MsgBox "Please select only 1 column."
            Set column1 = Application.InputBox("Select Column to Evaluate", Type:=8)
        Loop
    End If

    If column1.Rows.Count = 65536 Then
        Set column1 = Range(column1.Cells(1), column1.Cells(ActiveSheet.UsedRange.Rows.Count))
    End If

    For Each x In column1
        'Exit when we reach an empty cell
        If IsEmpty(x.Value) Then
            Exit Sub
        End If

        Set y = x.Offset(1, 0)
        Set z = x.Offset(2, 0)

        'Compare this cell to the next two in the column
        If x = y And x = z Then
            x.Interior.Color = vbYellow
            y.Interior.Color = vbYellow
            z.Interior.Color = vbYellow
        End If
    Next x
    End Sub