比较最近的细胞

我需要比较一个单元格和下一个,如果下一个比第一个大于3,比使它的颜色。 例如:1 2 6 3 2 8

1 compare with 2 = do not do nothing 2 compare with 6 = make it's color 6 compare with 3 = make it's color to 3 compare with 2 = do not do nothing 2 compare with 8 = make it's color. 

这里是使细胞less于4颜色的代码,但我不明白如何区分一个单元格与下一个:(

 Sub Color() Dim i As Integer For i = 1 To 7 With ActiveSheet.Cells(i) If .Value < 4 Then .Interior.Color = QBColor(10) End If End With Next i End Sub 

UPD:

哦! 看起来像我find解决scheme!

 Sub Color() Dim i As Integer For i = 1 To 7 With ActiveSheet.Cells(i) If ActiveSheet.Cells(i) < ActiveSheet.Cells(i + 1) Then ActiveSheet.Cells(i + 1).Interior.Color = QBColor(10) End If End With Next i End Sub 

你可以使用条件格式而不是VBA,Debra在这里详细讲述了这个话题, http://www.contextures.com/xlcondFormat01.html

在你的情况下:

  1. selectA1:E1
  2. 条件格式…新规则(不同的菜单选项取决于您的Excel版本)
  3. 使用公式来确定要格式化的单元格
  4. 使用= B1-A1> 3来添加一个相对的公式
  5. select一种填充颜色

从下面的xl2010截图 例