2017-03-15 4 views
3
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 

Le résultat est quand je reçois Selection.Value est vide, "-" , "-" et le reste tout montrer "Depot", "Allotment delay" seulement.J'utilise OU pour former une condition multiple IF ELSE sur VBA, il ne fonctionne pas

Quel est le problème avec ce code?

Répondre

6

En utilisant la ligne ci-dessous est incorrect:

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

Vous devez ajouter Selection.Value = avant chaque condition, voir la ligne ci-dessous:

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

Note: la même pour tous les autres ElseIf s vous avez .


Modifier 1

Cependant, je vous conseille d'utiliser le code ci-dessous. Votre code "crier" pour Select Case. En outre, il n'y a pas besoin de Range("AB" & cat).Select et plus tard utiliser ActiveCell, à la place, vous pouvez simplement utiliser Range entièrement qualifié.

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

Il fonctionne! Merci beaucoup. – lcc

+1

@lcc voir la réponse et le code sous ** Modifier 1 **, je pense que c'est la façon dont vous devez adapter à votre code –

+0

Avez-vous une explication de la façon dont le code original était même _running_? Je ne pouvais pas reproduire le comportement. –

3

Je pense que vous avez besoin d'exprimer l'égalité dans chaque condition. En d'autres termes, au lieu de cela:

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

vous devez utiliser:

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

Merci pour votre aide !! – lcc