我使用OR在VBA上形成一个多条件IF ELSE语句,它不起作用 [英] I use OR to form a multiple condition IF ELSE statement on VBA, it's not working

查看:1477
本文介绍了我使用OR在VBA上形成一个多条件IF ELSE语句,它不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 分配延迟

The result I get is when Selection.Value is empty, "-" , "-" and the rest all show "Depot" , "Allotment delay" only.

出了什么问题使用此代码?

What's wrong with this code ?

推荐答案

使用以下行不正确:

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

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

You need to add Selection.Value = before each condition, see line below:

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

注意:这同样适用于所有其他 ElseIf 你有。

Note: the same applies to all other ElseIfs you have.

编辑1

但是,我建议使用下面的代码。你的代码是尖叫选择案例。此外,不需要范围(AB和猫).Select 以及稍后使用 ActiveCell ,而不是您可以使用完全合格的范围

However,I would suggest to use the code below. Your code is "screaming" for Select Case. Also, there is no need to Range("AB" & cat).Select and later use ActiveCell, instead you could just use fully qualifed 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

这篇关于我使用OR在VBA上形成一个多条件IF ELSE语句,它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆