在使用excel VBA的If语句中不考虑条件

时间:2014-09-29 08:46:49

标签: excel vba if-statement

当以下代码被规定时,不考虑最后2个条件。你能帮忙理解为什么吗? 还有" r"范围检索我的预期结果+ 1排。我没有设法改变这个。有什么想法吗?

Dim x As Range
Dim r As Range
Dim j As Range

 Set r = Range("J2").End(xlDown)
 Set j = Range("D2").End(xlDown)
    For Each x In Range("J2", r)
     If x.Value < 0 And j = ("long") Or j = ("") Then
     x.Offset(1, 0).Select
     ActiveCell.FormulaR1C1 = "=R[-1]C+RC[-1]"
   End If
  Next
 End Sub

1 个答案:

答案 0 :(得分:0)

FYI:代码中的r范围将是从J2开始的列J的最后一个使用单元格。中间应该没有空单元格。

试试这个:

Sub posted()
Dim x As Range
Dim r As Range
'Dim j As Range

Set r = Range("J2").End(xlDown)
'Set j = Range("D2").End(xlDown)

For Each x In Range("J2", r)

If (x.Value < 0) And ((x.Offset(, -6).value = "long") _
Or (x.Offset(, -6).value = "")) Then

'''''''''dont know if you want cell value by long or number format of the cell''''
'If (x.Value < 0) And ((x.Offset(, -6).NumberFormat = "Number") _
Or (x.Offset(, -6) = "")) Then
'''''''''

x.Offset(1, 0).Select    
ActiveCell.FormulaR1C1 = "=R[-1]C+RC[-1]"
End If
Next
Set r = Nothing
End Sub