VBA:向ElseIf循环添加条件

时间:2017-10-20 14:25:48

标签: excel vba excel-vba loops

我想在这个循环中再添加一个条件,这将导致"差异"如果F列中的值既不是0也不是#VALUE,则进入J列。任何和所有建议将不胜感激。谢谢,目前的代码如下:

Sub ERS_Vlookup()

Dim Lastrow As Long
Dim h As Long

For h = 5 To Lastrow
        If IsError(ActiveSheet.Range("H" & h).Value) Or 
        IsError(ActiveSheet.Range("F" & h).Value) Then
            ActiveSheet.Range("J" & h).Value = " "
        ElseIf ActiveSheet.Range("H" & h).Value <> " " And _
               ActiveSheet.Range("F" & h).Value = 0 Then
            ActiveSheet.Range("J" & h).Value = "Paid"
        Else
            ActiveSheet.Range("J" & h).Value = "Processed Not Yet Paid"
        End If
    Next h

2 个答案:

答案 0 :(得分:0)

您可以使用And函数将布尔值添加到循环中 我建议你访问这个网站http://www.excel-easy.com/vba/loop.html 了解更多信息

答案 1 :(得分:0)

从表面上看,这应该有效,但我认为你需要仔细考虑并检查这些条件是否完全相互排斥。

Sub ERS_Vlookup()

Dim Lastrow As Long
Dim h As Long

For h = 5 To Lastrow
    If IsError(Range("H" & h).Value) Or IsError(Range("F" & h).Value) Then
        Range("J" & h).Value = vbNullString
    ElseIf Range("H" & h).Value <> vbNullString And Range("F" & h).Value = 0 Then
        Range("J" & h).Value = "Paid"
    ElseIf Not IsError(Range("F" & h).Value) And Range("F" & h).Value <> 0 Then
        Range("J" & h).Value = "Discrepancy"
    Else
        Range("J" & h).Value = "Processed Not Yet Paid"
    End If
Next h

End Sub