我使用OR在VBA上形成一个多条件IF ELSE语句,它不起作用

lcc*_*lcc 3 vba if-statement multiple-conditions

Dim cat As Integer
For cat = 2 To last
    Range("AB" & cat).Select

    If Selection.Value = " " Then
        ActiveCell.Offset(0, -2).Value = "-"
        ActiveCell.Offset(0, -1).Value = "-"

    ElseIf Selection.Value = "Address in local wording" Then
        ActiveCell.Offset(0, -2).Value = "Customer"
        ActiveCell.Offset(0, -1).Value = "Incomplete information or awaiting more info from customer"

    ElseIf (Selection.Value = "hold to console" Or "Hold to console" Or "Allocated 14/12 and ship next day") Then
        ActiveCell.Offset(0, -2).Value = "Depot"
        ActiveCell.Offset(0, -1).Value = "Allotment delay"

    ElseIf (Selection.Value = "Backorder" Or "backorder" Or "Back order" Or "back order") Then
        ActiveCell.Offset(0, -2).Value = "Inventory"
        ActiveCell.Offset(0, -1).Value = "Material not available causing backorder"

    End If        
Next cat
Run Code Online (Sandbox Code Playgroud)

结果我得到的是当Selection.Value是空的, "-" , "-" 剩下的所有节目"Depot","Allotment delay"只.

这段代码出了什么问题?

Sha*_*ado 7

使用以下行是不正确的:

ElseIf (Selection.Value = "hold to console" Or "Hold to console" Or "Allocated 14/12 and ship next day") Then
Run Code Online (Sandbox Code Playgroud)

您需要Selection.Value =在每个条件之前添加,请参阅下面的行:

ElseIf Selection.Value = "hold to console" Or Selection.Value = "Hold to console" Or Selection.Value = "Allocated 14/12 and ship next day" Then
Run Code Online (Sandbox Code Playgroud)

注意:这同样适用于ElseIf您拥有的所有其他.


编辑1

但是,我建议使用下面的代码.你的代码是"尖叫"的Select Case.此外,没有必要Range("AB" & cat).Select和以后使用ActiveCell,相反,你可以使用完全合格Range.

Dim cat As Long

For cat = 2 To last
    Select Case Range("AB" & cat).Value
        Case " "
            Range("AB" & cat).Offset(0, -2).Value = "-"
            Range("AB" & cat).Offset(0, -1).Value = "-"

        Case "Address in local wording"
            Range("AB" & cat).Offset(0, -2).Value = "Customer"
            Range("AB" & cat).Offset(0, -1).Value = "Incomplete information or awaiting more info from customer"

        Case "hold to console", "Hold to console", "Allocated 14/12 and ship next day"
            Range("AB" & cat).Offset(0, -2).Value = "Depot"
            Range("AB" & cat).Offset(0, -1).Value = "Allotment delay"

        Case "Backorder", "backorder", "Back order", "back order"
            Range("AB" & cat).Offset(0, -2).Value = "Inventory"
            Range("AB" & cat).Offset(0, -1).Value = "Material not available causing backorder"
    End Select

Next cat
Run Code Online (Sandbox Code Playgroud)