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

时间:2017-07-21 21:26:40

标签: excel vba highlight

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

  1. A
  2. A
  3. Ñ
  4. P
  5. P
  6. P
  7. Ñ
  8. 3 P将突出显示为红色,但不是2 A或2B。这是否可以在VBA中使用或使用公式我不知道,但我一直试图让我的头围绕它一段时间。我确定它可能很简单,我只是陷入了一个循环

    任何帮助都会非常感激

3 个答案:

答案 0 :(得分:1)

条件格式化有点棘手。您可以选择H3并尝试以下公式:

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

然后将“适用于”更改为整个范围。

<强>更新

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

答案 1 :(得分:0)

只要您的数据开始处有几行......

enter image description here

答案 2 :(得分:0)

我没有进行过多次测试的快速代码,但试试这个:

    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