PROBLÈME MACRO = If imbriqué en VBA

Bonjour,

Je ne comprends pas ma macro se lance plus : il me dit "Next sans For"

Sub test1()
Dim relance As Worksheet
Dim suiv As Worksheet
Dim codepayeur As String
Dim sin As String
Dim montant As Currency

'Affectation variable avec les onglets

Set relance = Sheets("RELANCES CIES CORPOREL")
Set suiv = Sheets("SUIVTRANS RELANCES BCF MANDAT")

suiv.Activate

Dernligne = suiv.Range("H" & Rows.Count).End(xlUp).Row
relance.Activate

For j = 2 To Dernligne

'sasie donées
    If Cells(j, 5) = "FRA-BCF" Then
        'Si FRA-BCF
        relance.Cells(j, 1) = "RELANCE_BCF GESTION_ FRA-BCF" & "_" & relance.Cells(j, 6).Value & relance.Cells(j, 4).Value & "/" & relance.Cells(j, 5).Value & ""
    If Cells(j, 5) = "MANDAT RELANCE CIE" Then
        'Si autre que FRA-BCF
        relance.Cells(j, 1) = "RELANCE MANDAT" & "_" & relance.Cells(j, 3).Value & "_" & relance.Cells(j, 5).Value & relance.Cells(j, 6).Value & "_" & relance.Cells(j, 5).Value & "_" & " Bodily claim " & relance.Cells(j, 4).Value
        '[Nom compagnie] Bodily(Material) claim [n°sin AZ]/[ref compagnie] pending list Q1 2019 AZ France
    If Cells(j, 5) = "MANDAT AG" Then
    relance.Cells(j, 1) = "AG BCF MANDAT" & "_" & relance.Cells(j, 3).Value & "_" & relance.Cells(j, 5).Value & relance.Cells(j, 6).Value & "_" & relance.Cells(j, 5).Value & "_" & " Bodily claim " & relance.Cells(j, 4).Value

    If Cells(j, 5) = "MANDAT RELANCE AG" Then
    relance.Cells(j, 1) = "RELANCE AG BCF MANDAT" & "_" & relance.Cells(j, 3).Value & "_" & relance.Cells(j, 5).Value & relance.Cells(j, 6).Value & "_" & relance.Cells(j, 5).Value & "_" & " Bodily claim " & relance.Cells(j, 4).Value

   End If
   Next j
End Sub

Bonjour,

Je suis sur tél alors... Mais indentez votre code et vous verrez que c'est plus un manque de End If qu'un Next sans For... Mais pas sur...

@ bientôt

LouReeD

Bonjour,

Comme l'a dit LouReeD, c'est un problème de End If ... Tes Ifs ne sont pas fermés (ou tu as utilisé des If au lieu de ElseIf).

Si tu veux te passer de End If, ça ne marche que si c'est sur une ligne :

For j = 2 To Dernligne
    'Si FRA-BCF
    If Cells(j, 5) = "FRA-BCF" Then relance.Cells(j, 1) = "RELANCE_BCF GESTION_ FRA-BCF" & "_" & relance.Cells(j, 6) & relance.Cells(j, 4) & "/" & relance.Cells(j, 5) & ""
    'Si autre que FRA-BCF
    If Cells(j, 5) = "MANDAT RELANCE CIE" Then relance.Cells(j, 1) = "RELANCE MANDAT" & "_" & relance.Cells(j, 3) & "_" & relance.Cells(j, 5) & relance.Cells(j, 6) & "_" & relance.Cells(j, 5) & "_" & " Bodily claim " & relance.Cells(j, 4)
    '[Nom compagnie] Bodily(Material) claim [n°sin AZ]/[ref compagnie] pending list Q1 2019 AZ France
    If Cells(j, 5) = "MANDAT AG" Then relance.Cells(j, 1) = "AG BCF MANDAT" & "_" & relance.Cells(j, 3) & "_" & relance.Cells(j, 5) & relance.Cells(j, 6) & "_" & relance.Cells(j, 5) & "_" & " Bodily claim " & relance.Cells(j, 4)
    If Cells(j, 5) = "MANDAT RELANCE AG" Then relance.Cells(j, 1) = "RELANCE AG BCF MANDAT" & "_" & relance.Cells(j, 3) & "_" & relance.Cells(j, 5) & relance.Cells(j, 6) & "_" & relance.Cells(j, 5) & "_" & " Bodily claim " & relance.Cells(j, 4)
Next

Si tu veux mettre un seul End If, tu dois utiliser des ElseIf au milieu :

For j = 2 To Dernligne
    'Si FRA-BCF
    If Cells(j, 5) = "FRA-BCF" Then
        relance.Cells(j, 1) = "RELANCE_BCF GESTION_ FRA-BCF" & "_" & relance.Cells(j, 6) & relance.Cells(j, 4) & "/" & relance.Cells(j, 5) & ""
    'Si autre que FRA-BCF
    ElseIf Cells(j, 5) = "MANDAT RELANCE CIE" Then
        relance.Cells(j, 1) = "RELANCE MANDAT" & "_" & relance.Cells(j, 3) & "_" & relance.Cells(j, 5) & relance.Cells(j, 6) & "_" & relance.Cells(j, 5) & "_" & " Bodily claim " & relance.Cells(j, 4)
    '[Nom compagnie] Bodily(Material) claim [n°sin AZ]/[ref compagnie] pending list Q1 2019 AZ France
    ElseIf Cells(j, 5) = "MANDAT AG" Then
        relance.Cells(j, 1) = "AG BCF MANDAT" & "_" & relance.Cells(j, 3) & "_" & relance.Cells(j, 5) & relance.Cells(j, 6) & "_" & relance.Cells(j, 5) & "_" & " Bodily claim " & relance.Cells(j, 4)
    ElseIf Cells(j, 5) = "MANDAT RELANCE AG" Then
        relance.Cells(j, 1) = "RELANCE AG BCF MANDAT" & "_" & relance.Cells(j, 3) & "_" & relance.Cells(j, 5) & relance.Cells(j, 6) & "_" & relance.Cells(j, 5) & "_" & " Bodily claim " & relance.Cells(j, 4)
    End If
Next

Ou mieux encore dans ton cas :

For j = 2 To Dernligne
    Select Case Cells(j, 5)
    'Si FRA-BCF
    Case Is = "FRA-BCF"
        relance.Cells(j, 1) = "RELANCE_BCF GESTION_ FRA-BCF" & "_" & relance.Cells(j, 6) & relance.Cells(j, 4) & "/" & relance.Cells(j, 5) & ""
    'Si autre que FRA-BCF
    Case Is = "MANDAT RELANCE CIE"
        relance.Cells(j, 1) = "RELANCE MANDAT" & "_" & relance.Cells(j, 3) & "_" & relance.Cells(j, 5) & relance.Cells(j, 6) & "_" & relance.Cells(j, 5) & "_" & " Bodily claim " & relance.Cells(j, 4)
    '[Nom compagnie] Bodily(Material) claim [n°sin AZ]/[ref compagnie] pending list Q1 2019 AZ France
    Case Is = "MANDAT AG"
        relance.Cells(j, 1) = "AG BCF MANDAT" & "_" & relance.Cells(j, 3) & "_" & relance.Cells(j, 5) & relance.Cells(j, 6) & "_" & relance.Cells(j, 5) & "_" & " Bodily claim " & relance.Cells(j, 4)
    Case Is = "MANDAT RELANCE AG"
        relance.Cells(j, 1) = "RELANCE AG BCF MANDAT" & "_" & relance.Cells(j, 3) & "_" & relance.Cells(j, 5) & relance.Cells(j, 6) & "_" & relance.Cells(j, 5) & "_" & " Bodily claim " & relance.Cells(j, 4)
    End Select
Next

Cordialement,

Bonjour,

Merci Sébastien pour t'es explications !

J'ai un autre petit problème sur le codage suivant, un message s'affiche en me disant " Erreur de compilation : Next sans for", alors que j'ai mis le For avant ...

With Sheets("SUIVTRANS EN COURS")
    Derligne = .Range("A" & Rows.Count).End(xlUp).Row
    .Range("M3:M" & Derligne).ClearContents

For j = 3 To Derligne

       'ANNULATION TECHNIQUE (5)
        If Mid(.Cells(j, 6).Text, 5, 1) = "A" And Mid(.Cells(j, 6).Text, 1, 1) = "S" Then
        .Cells(j, 12).Value = "ANNULATION TECHNIQUE"

       If .Cells(j, 12).Value = "ANNULATION TECHNIQUE" Then
       .Cells(j, 13).Value = "ANNULATION TECHNIQUE"

       If .Cells(j, 9) = "REGLT" And .Cells(j, 18) And .Cells(j, 12) <> "RECOURS MATERIEL" Or .Cells(j, 12) <> "RECOURS CORPOREL" Or .Cells(j, 12) <> "RECOURS RC" Then
            .Cells(j, 13) = "SOLDE CREDITEUR - A REVOIR"
       End If

        'REGULARISATION ECART-TEMPLATE (2)
        'If somme >= -10 And somme <= 10 And somme <> 0 Then
            '.Cells(j, 13) = "REGULARISATION ECART-TEMPLATE"
         ElseIf .Cells(j, "R") >= -10 And .Cells(j, "R") <= 10 And .Cells(j, "R") <> 0 Then
            .Cells(j, 13) = "REGULARISATION ECART-TEMPLATE"

        'SOLDE CREDITEUR - A REMBOURSER (3)
        'ElseIf somme < -10 Then
            '.Cells(j, 13) = "SOLDE CREDITEUR - A REMBOURSER"
        ElseIf .Cells(j, 9) = "REGLT" And .Cells(j, "R") < -10 Then
            .Cells(j, 13) = "SOLDE CREDITEUR - A REMBOURSER"

        'REGLT CIE - SOLDE DEBITEUR (4)
        'ElseIf .Cells(j, 9) = "REGLT" And somme > 10 Then
            '.Cells(j, 13) = "REGLT CIE-SOLDE-DEBITEUR"
         ElseIf .Cells(j, 9) = "REGLT" And Cells(j, "R") > 10 Then
            .Cells(j, 13) = "REGLT CIE-SOLDE-DEBITEUR"

      End If

    Next j

End With

Bonjour tout le monde,

Le sujet à pourtant déjà été traité : https://forum.excel-pratique.com/viewtopic.php?f=2&t=121998&start=10

Il existe plusieurs syntaxes possibles avec "If" :

1 ligne :

If condition Then Instruction 'Pas besoin de "End If" dans ce cas

Plusieurs lignes :

If condition Then
   Instruction
   Instruction2 'facultatif
Else 'facultatif
   'Instruction autre
End If

Il faut donc que tu sautes une ligne dans cette partie après "Then" :

If .Cells(j, "R") >= -10 And .Cells(j, "R") <= 10 And .Cells(j, "R") <> 0 Then .Cells(j, 13) = "REGULARISATION ECART-TEMPLATE"
'Devient :
If .Cells(j, "R") >= -10 And .Cells(j, "R") <= 10 And .Cells(j, "R") <> 0 Then 
   .Cells(j, 13) = "REGULARISATION ECART-TEMPLATE"
Elseif Truc '... 

A part la syntaxe 1 de la citation ci-dessus, il faut mettre autant de "End If" qu'on a mis de "If"

Rechercher des sujets similaires à "probleme macro imbrique vba"