Excel 比较最近的单元格

Excel 比较最近的单元格,excel,vba,Excel,Vba,我需要将一个单元格与下一个单元格进行比较,如果下一个单元格比第一个单元格大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.

我需要将一个单元格与下一个单元格进行比较,如果下一个单元格比第一个单元格大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.
下面是使单元格颜色小于4的代码,但我不明白如何区分一个单元格和下一个单元格:(

Sub-Color()
作为整数的Dim i
对于i=1到7
使用ActiveSheet.Cells(i)
如果.Value<4,则
.Interior.Color=QB颜色(10)
如果结束
以
接下来我
端接头
Upd:

哦!看来我找到解决办法了

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
Sub-Color()
作为整数的Dim i
对于i=1到7
使用ActiveSheet.Cells(i)
如果ActiveSheet.Cells(i)
您可以使用条件格式,而不是VBA,Debra在这里详细介绍了这个主题

就你而言:

  • 选择A1:E1
  • 条件格式…新规则(不同的菜单选项取决于您的Excel版本)
  • 使用公式确定要格式化的单元格
  • 使用=B1-A1>3添加相对公式
  • 选择填充颜色
  • 下面是xl2010的截图

    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