Else sans If

Résolu/Fermé
S9091 Messages postés 7 Date d'inscription lundi 11 juillet 2016 Statut Membre Dernière intervention 13 juillet 2016 - Modifié par pijaku le 12/07/2016 à 10:42
S9091 Messages postés 7 Date d'inscription lundi 11 juillet 2016 Statut Membre Dernière intervention 13 juillet 2016 - 12 juil. 2016 à 11:11
Bonjour ,
Pourrez vous me dire quelle est la faute dans ce code ??? lors de la compilation un message " else sans if" s'affiche au niveau de la première elseif . Voici le code :

DPass = CDate(Range("M2"))
If Application.WorksheetFunction.NETWORKSDAYS(M2, M2 + Application.WorksheetFunction.VLookup(P2, Worksheet.Produits!("A2:C169"), 3, 0), 3, 0, Worksheet.JoursfériésTunisie!("B2:B13")) = Application.WorksheetFunction.VLookup(P2, Worksheet.Produits!("A2:C169"), 3, 0) Then
SLA = CDate(M2 + Application.WorksheetFunction.VLookup(P2, Worksheet.Produits!("A2:C169"), 3, 0))
Else: SLA = CDate(M2 + 2 * Application.WorksheetFunction.VLookup(P2, Worksheet.Produits!("A2:C169"), 3, 0) - Application.WorksheetFunction.NETWORKSDAYS(M2, M2 + Application.WorksheetFunction.VLookup(P2, Worksheet.Produits!("A2:C169"), 3, 0), 3, 0, Worksheet.JoursfériésTunisie!("B2:B13")))
End If

If Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 31, 0) = "YES" Then D = "lundi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 32, 0) = "YES" Then D = "mardi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 33, 0) = "YES" Then D = "mercredi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 34, 0) = "YES" Then D = "jeudi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 35, 0) = "YES" Then D = "vendredi"
Else: D = "samedi"
End If

merci d'avance :)

1 réponse

pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 744
12 juil. 2016 à 10:44
Bonjour,

Remplacez :
If Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 31, 0) = "YES" Then D = "lundi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 32, 0) = "YES" Then D = "mardi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 33, 0) = "YES" Then D = "mercredi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 34, 0) = "YES" Then D = "jeudi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 35, 0) = "YES" Then D = "vendredi"
Else: D = "samedi"
End If

Par :
If Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 31, 0) = "YES" Then 
    D = "lundi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 32, 0) = "YES" Then 
    D = "mardi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 33, 0) = "YES" Then 
    D = "mercredi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 34, 0) = "YES" Then 
    D = "jeudi"
ElseIf Application.WorksheetFunction.VLookup(E2, Worksheet.Clients!("A1:AJ836"), 35, 0) = "YES" Then 
    D = "vendredi"
Else: D = "samedi"
End If


==> nouvelle ligne après chaque Then.
2
Stib_13 Messages postés 60 Date d'inscription mercredi 13 mars 2013 Statut Membre Dernière intervention 8 avril 2018 384
12 juil. 2016 à 10:47
Ah oui effectivement, c'est cette méthode qu'il faut suivre !
0
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 744 > Stib_13 Messages postés 60 Date d'inscription mercredi 13 mars 2013 Statut Membre Dernière intervention 8 avril 2018
Modifié par pijaku le 12/07/2016 à 10:49
En fait, la syntaxe de
If test Then reponse
en une seule ligne clôture le test If.
Comme ici, il souhaite continuer le test par des ElseIf, il convient d'utiliser la syntaxe sur plusieurs lignes, soit :
If test Then
reponse
ElseIf test2 Then
reponse2
etc...
0
S9091 Messages postés 7 Date d'inscription lundi 11 juillet 2016 Statut Membre Dernière intervention 13 juillet 2016
12 juil. 2016 à 10:51
merci beaucoup le problème est résolu mais maintenant lors de l'exécution un message "objet requis " apparait et la deuxième ligne est surlignée : If Application.WorksheetFunction.NETWORKSDAYS(M2, M2 + Application.WorksheetFunction.VLookup(P2, Worksheet.Produits!("A2:C169"), 3, 0), 3, 0, Worksheet.JoursfériésTunisie!("B2:B13")) = Application.WorksheetFunction.VLookup(P2, Worksheet.Produits!("A2:C169"), 3, 0) Then

avez vous une idée ???
0
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 744 > S9091 Messages postés 7 Date d'inscription lundi 11 juillet 2016 Statut Membre Dernière intervention 13 juillet 2016
12 juil. 2016 à 11:01
Oui, j'ai une idée.
Application.WorksheetFunction.NETWORKSDAYS(M2, M2 + Application.WorksheetFunction.VLookup(P2, Worksheet.Produits!("A2:C169"), 3, 0), 3, 0, Worksheet.JoursfériésTunisie!("B2:B13"))
Mais comme cette formule ne me dis rien, voici la marche à suivre :
1- saisir votre formule excel telle qu'elle serait normalement utilisée (sans VBA) dans une cellule,
2- sélectionnez cette cellule
3- Tapez Alt+F11 (==>VBA)
4- Tapez Ctrl+G (==> la fenêtre d'exécution apparaît)
5- dans la fenêtre d'exécution saisir :
? ActiveCell.Formula
6- valider par la touche Entrée

revenez ici copier/coller la formule obtenue dans la fenêtre d'exécution.
0
S9091 Messages postés 7 Date d'inscription lundi 11 juillet 2016 Statut Membre Dernière intervention 13 juillet 2016
12 juil. 2016 à 11:11
voici le code de la formule avec Active.Cell mais je sais pas comment l'associer à la variable SLA afin de l'utiliser dans le reste du code

Sub date_promise()
'
' date_promise Macro
'

'
ActiveCell.FormulaR1C1 = _
"=IF(NETWORKDAYS(RC[-2],RC[-2]+VLOOKUP(RC[1],Produits!R2C1:R169C3,3,0),'Jours fériés Tunisie'!RC[-13]:R[11]C[-13])=VLOOKUP(RC[1],Produits!R2C1:R169C3,3,0),RC[-2]+VLOOKUP(RC[1],Produits!R2C1:R169C3,3,0),RC[-2]+2*VLOOKUP(RC[1],Produits!R2C1:R169C3,3,0)-NETWORKDAYS(RC[-2],RC[-2]+VLOOKUP(RC[1],Produits!R2C1:R169C3,3,0),'Jours fériés Tunisie'!RC[-13]:R[11]C[-13]))"
Range("O2").Select
Selection.AutoFill Destination:=Range("O2:O1997")
Range("O2:O1997").Select
End Sub
0