Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

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

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 ?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using the line below is incorrect:

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

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

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


Edit 1

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.

Code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...