为一系列增长的单元格的最后一个单元格颜色

我正在尝试创build一个比上一个更高的单元格的macros。 我想只着色跟随一系列30个单元格的单元格,每个单元格比上一个更高。 在这个截图中,如果我有这样一个系列,只有E35应该是彩色的,因为从E5到E35,这30个单元中的每一个都严格高于它们的前任(E35> E34> E33> …> E6> E5)。

在这里输入图像说明

这是我试图做的代码:

Sub Consecutive_HigherCells() Dim i, j As Integer For i = 32 to 10000 For j = 1 To 30 If Cells (i,5).Value > Cells(ij,5).Value Then Cells(i, 5).Select With Selection.Font .Color = -16776961 .TintAndShade = 0 End With End If Next j Next i End Sub 

实际上代码不工作,因为从E32直到E1000的所有单元都至less高于30个重要单元中的一个单元时,我运行它。

我真的需要你的帮助

 Option explicit Sub Consecutive_HigherCells() Const LIMIT as long = 30 Dim i as long, j as long, Counter as long For i = 32 to 10000 Counter = 0 For j = LIMIT to 1 step -1 If cells(ij-1,"E").Value2 > cells(ij,"E").value2 Then Counter = counter + 1 Else Exit for End if Next j If counter = LIMIT then cells(i,"E").interior.color = rgb(255,255,0) Next i End Sub 

未经testing并在手机上书写,抱歉格式不对。

下面的代码会遍历整个列表和颜色单元格,其中下一个单元格的值较低

 Sub HighlightCells30() Dim lr As Long, i As Long, count As Long count = 0 lr = ActiveSheet.Range("E" & Rows.count).End(xlUp).Row For i = 5 To lr count = count + 1 If Range("E" & i + 1).Value < Range("E" & i).Value Then If i <> lr And count > 30 Then Range("E" & i).Interior.Color = vbYellow count = 0 End If End If Next i End Sub 

我没有完全弄清楚30个批次试图达到什么目的? 编辑:基于下面的Scotts解释更新的代码

@Chillin>感谢您的帮助,您近在咫尺。 我修改了你的代码,现在正在工作。

 Option Explicit Sub Consecutive_HigherCells30() Const LIMIT As Long = 30 Dim i As Long, j As Long, Counter As Long For i = 32 To 10000 Counter = 0 For j = LIMIT To 1 Step -1 'If Cells(i - j - 1, "E").Value > Cells(i - j, "E").Value Then If Cells(i - j - 1, "E").Value < Cells(i - j, "E").Value Then Counter = Counter + 1 Else Exit For End If Next j If Counter = LIMIT Then Cells(i - 1, "E").Interior.Color = RGB(255, 255, 0) Next i End Sub