如果大于不同位置的前一个单元格

时间:2016-11-23 15:58:21

标签: excel vba

我必须检查,如果单元格“C”中的值大于0但小于PREVIOUS单元格“E”中的值,例如,检查单元格“3C”的值是否大于0 ,但小于单元格“2E”中的值。我已经放入RGB为它着色包括单元格红色的错误。我也使用UserForms进行输入。

这是我在Excel VBA中的代码:

    X = Cells(j, "C").Value 
    d = Cells(j, "E").Value
    c = Cells(j, "E-1").Value
    If X >= c And X < 0 Or X < 0 Or X >= c Then
            Cells(j, "C").Interior.Color = 255
            UserForm1.inpBox.Value = j
            UserForm1.Label1.Caption = (j & ". Row has an error!")
            UserForm1.Show
            Cells(j, "C") = UserForm1.inpBox.Value
            Cells(j, "C").Interior.Color = RGB(255, 255, 255)
        End If

我知道这里有一个错误,但我无法弄明白,该放置什么工作才能让它发挥作用:

    X = Cells(j, "C").Value 
    d = Cells(j, "E").Value
    c = Cells(j, "E-1").Value
    If X >= c And X < 0 Or X < 0 Or X >= c Then

1 个答案:

答案 0 :(得分:0)

使用Range.Offset(rowOffset, colOffset)方法:

c = Cells(j, "E").Offset(0, -1).Value  ' addresses "previous" column
c = Cells(j, "E").Offset(-1, 0).Value  ' addresses "previous" row