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

前端 未结 2 1092
一向
一向 2020-11-28 16:22
Dim cat As Integer
For cat = 2 To last
    Range(\"AB\" & cat).Select

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


        
相关标签:
2条回答
  • 2020-11-28 16:34

    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
    
    0 讨论(0)
  • 2020-11-28 16:38

    I think you need to express equality in each condition. In other words, instead of this:

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

    you need to use this:

    (Selection.Value = "hold to console" Or
     Selection.Value = "Hold to console" Or
     Selection.Value = "Allocated 14/12 and ship next day") Then
    
    0 讨论(0)
提交回复
热议问题