@ScottCraner 解决方案绝对是首选,但我喜欢我的 VBA 解决方案,所以我将它加入其中。
代码假定您只有 X 和 O,但会为任何 10 或更多的重复值着色。
Public Sub Test()
Dim rLastCell As Range
Dim rCell As Range
Dim rFirstCell As Range
Dim rCurrentCell As Range
Set rLastCell = LastCell(ThisWorkbook.Worksheets("Sheet1"))
With ThisWorkbook.Worksheets("Sheet1")
'A For Each will step through each cell going across the columns and then down the rows.
'Just need to reset if the it's the first column and check if the next cell is equal to the previous
'and reset when it changes.
For Each rCell In .Range(.Cells(1, 1), rLastCell)
If rCell.Column = 1 Then
Set rFirstCell = rCell
ElseIf rCell.Value <> rFirstCell.Value Then
If rCell.Column - rFirstCell.Column >= 10 Then
rFirstCell.Resize(, rCell.Column - rFirstCell.Column).Interior.Color = RGB(255, 0, 0)
End If
Set rFirstCell = rCell
End If
Next rCell
End With
End Sub
Public Function LastCell(wrkSht As Worksheet, Optional Col As Long = 0) As Range
Dim lLastCol As Long, lLastRow As Long
On Error Resume Next
With wrkSht
If Col = 0 Then
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
Else
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Columns(Col).Find("*", , , , xlByColumns, xlPrevious).Row
End If
If lLastCol = 0 Then lLastCol = 1
If lLastRow = 0 Then lLastRow = 1
Set LastCell = wrkSht.Cells(lLastRow, lLastCol)
End With
On Error GoTo 0
End Function