2016-11-23 58 views
0

我必須檢查,如果單元格「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 

回答

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 
+0

我寫了這一點,它似乎工作:'C =細胞(j - 1, 「E」)Value'你認爲怎麼樣這個版本? –

+0

當您提供數字行和列時,這也會起作用。請注意,在您的「不工作」示例中,您嘗試解決前一個*列*'「E-1」',而在您評論中,您從*行*中減去一個。如果您使用命名範圍或將列提供爲字母,則「偏移量」方法將起作用。兩者都很好。 –

+0

非常感謝你! :) –

相關問題