问题
I am trying to create a macro that color cells that are higher than the previous one. I want to color only cells that follow a serie of 5 cells each one hiher than the previous one. In this screenshot, if I have such a series, only D14 should be colored in red, because D14>D13>D12>D11>D10>D9. In other terms, because from D9 to D13, the 5 cells are always higher than the previous one.
This is my code but it is not working:
Sub Consecutive_HigherCells()
Dim i, j As Integer
For j = 1 To 5
    If Cells (i,4).Value >> Cells(i-j,4).Value Then
    Cells(i, 4).Select
    With Selection.Font
        .Color = -16776961
        .TintAndShade = 0
    End With
    End If
Next j
End Sub
Thanks for helping me
回答1:
Clearly the first candidate is cell A6 (since it has 5 predecessors):
Sub BetterRedThanDead()
    Dim i As Long, N As Long
    N = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 6 To N
        If boo(Cells(i - 1, 1), Cells(i - 2, 1), Cells(i - 3, 1), Cells(i - 4, 1), Cells(i - 5, 1)) Then
            Cells(i, 1).Interior.ColorIndex = 3
        End If
    Next i
End Sub
Public Function boo(a, b, c, d, e) As Boolean
    boo = False
    If a > b And b > c And c > d And d > e Then boo = True
End Function
For example:
回答2:
You are missing to declare i variable. also >> is not the right way to compare values.
Line should be:
If Cells (i,4).Value > Cells(i-j,4).Value Then
Also you need to add something like
If j = 5 Then 'It implies your validation is success
 With Selection.Font
        .Color = -16776961
        .TintAndShade = 0
    End With
End If
来源:https://stackoverflow.com/questions/46955969/color-consecutive-higher-cells