【问题标题】:I use OR to form a multiple condition IF ELSE statement on VBA, it's not working我使用 OR 在 VBA 上形成多个条件 IF ELSE 语句,它不起作用
【发布时间】:2017-08-05 20:09:33
【问题描述】:
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"

这段代码有什么问题?

【问题讨论】:

    标签: vba if-statement multiple-conditions


    【解决方案1】:

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

    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
    

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


    编辑 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
    

    【讨论】:

    • @lcc 查看Edit 1下的答案和代码,我认为这是你应该适应你的代码的方式
    • 您对原始代码是如何运行有解释吗?我无法重现该行为。
    【解决方案2】:

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

    (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
    

    【讨论】:

    • 感谢您的帮助!!
    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2019-03-01
    • 2017-07-02
    • 2014-03-21
    • 1970-01-01
    相关资源
    最近更新 更多