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

I use OR to form a multiple condition IF ELSE statement on VBA, it's not working

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

我得到的结果是当Selection.Value为空时,"-" , "-" 其余的都只显示 "Depot" , "Allotment delay".

这段代码有什么问题?

使用下面的行是不正确的:

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

您需要在每个条件前添加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

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


编辑 1

不过,我建议使用下面的代码。您的代码是 "screaming" for 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

我认为你需要在每个条件下表达平等。换句话说,而不是这个:

(Selection.Value = "hold to console" Or
 "Hold to console" Or
 "Allocated 14/12 and ship next day") Then

你需要使用这个:

(Selection.Value = "hold to console" Or
 Selection.Value = "Hold to console" Or
 Selection.Value = "Allocated 14/12 and ship next day") Then