Erreur syntaxe boucle for / next

Bonjour à tous

j'ai un fichier avec une macro qui permet d'intégrer des données d'une feuille vers une autre, selon des conditions

Je ne comprend pas , lorsque je souhaite lancer la macro, le code detecte un NExt, sans le FOR

Pourtant il est bien dans le code.

Et ca a l'air au bon endroit…

Voiila un bout du code =>

Sub test()
Dim derLig As Long, i As Long, lig As Long
mois = Month(Date)
MsgBox (mois)

With Sheets("données à intégrer")
derLig = .Cells(.Rows.Count, 1).End(xlUp).Row

For i = 4 To derLig

On Error Resume Next
lig = Sheets("Logt Attribués 2021").Range("A4:A10000").Find(.Range("A" & i), LookIn:=xlValues, Lookat:=xlWhole).Row
If lig = 0 And mois = 6 Then
.Range("A" & i & ":AA" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.Range("Q" & i & ":R" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 23).End(xlUp).Offset(1, 0)

Else
If lig = 0 And mois = 11 Then
.Range("A" & i & ":AA" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.Range("Q" & i & ":R" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 36).End(xlUp).Offset(1, 0)

Else
If lig = 0 And mois = 12 Then
.Range("A" & i & ":AA" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
.Range("Q" & i & ":R" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 38).End(xlUp).Offset(1, 0)

Else
If lig <> 0 And mois = 6 Then
Sheets("Logt attribués 2021").Range("S" & lig & ":" & "AA" & lig) = .Range("S" & i & ":AA" & i).Value
Sheets("Logt attribués 2021").Range("AB" & lig & ":" & "AC" & lig) = .Range("AB" & i & ":AC" & i).Value


Else
If lig <> 0 And mois = 12 Then
Sheets("Logt attribués 2021").Range("S" & lig & ":" & "AA" & lig) = .Range("S" & i & ":AA" & i).Value
Sheets("Logt attribués 2021").Range("AN" & lig & ":" & "AO" & lig) = .Range("AB" & i & ":AC" & i).Value
End If

lig = 0

Next i

End With
End Sub

Auriez vous une explication svp ?

Je reste dispo si besoin de plus de détails

Au passage, Merci à tous pour votre soutien ^^

Bonjour,

Même si le compilateur te donne cette erreur, ce n'est pas forcément la cause...

Regarde plutôt les lignes If, Else ...

en remplaçant les différents Else par ElseIf, ça se compile (non testé sur ton fichier)

Sub test()
Dim derLig As Long, i As Long, lig As Long
mois = Month(Date)
MsgBox (mois)

With Sheets("données à intégrer")
    derLig = .Cells(.Rows.Count, 1).End(xlUp).Row

    For i = 4 To derLig

        On Error Resume Next
        lig = Sheets("Logt Attribués 2021").Range("A4:A10000").Find(.Range("A" & i), LookIn:=xlValues, Lookat:=xlWhole).Row
        If lig = 0 And mois = 6 Then
            .Range("A" & i & ":AA" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
            .Range("Q" & i & ":R" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 23).End(xlUp).Offset(1, 0)

        ElseIf lig = 0 And mois = 11 Then
            .Range("A" & i & ":AA" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
            .Range("Q" & i & ":R" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 36).End(xlUp).Offset(1, 0)

        ElseIf lig = 0 And mois = 12 Then
            .Range("A" & i & ":AA" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
            .Range("Q" & i & ":R" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 38).End(xlUp).Offset(1, 0)

        ElseIf lig <> 0 And mois = 6 Then
            Sheets("Logt attribués 2021").Range("S" & lig & ":" & "AA" & lig) = .Range("S" & i & ":AA" & i).Value
            Sheets("Logt attribués 2021").Range("AB" & lig & ":" & "AC" & lig) = .Range("AB" & i & ":AC" & i).Value

        ElseIf lig <> 0 And mois = 12 Then
            Sheets("Logt attribués 2021").Range("S" & lig & ":" & "AA" & lig) = .Range("S" & i & ":AA" & i).Value
            Sheets("Logt attribués 2021").Range("AN" & lig & ":" & "AO" & lig) = .Range("AB" & i & ":AC" & i).Value
        End If

    lig = 0

    Next i

End With
End Sub

Salut,

lors de ton programme, tu met des nouveau If sans mettre de end if, il pense donc que tu as un next dans une condition If

pour corriger cela tu as juste à mettre des elseif au lieu des else puis if

    For i = 4 To derLig

        On Error Resume Next

        lig = Sheets("Logt Attribués 2021").Range("A4:A10000").Find(.Range("A" & i), LookIn:=xlValues, Lookat:=xlWhole).Row

        If lig = 0 And mois = 6 Then
        .Range("A" & i & ":AA" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        .Range("Q" & i & ":R" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 23).End(xlUp).Offset(1, 0)

        ElseIf lig = 0 And mois = 7 Then
        .Range("A" & i & ":AA" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        .Range("Q" & i & ":R" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 28).End(xlUp).Offset(1, 0)

        ElseIf lig = 0 And mois = 8 Then
        .Range("A" & i & ":AA" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        .Range("Q" & i & ":R" & i).Copy Sheets("Logt attribués 2021").Cells(Rows.Count, 30).End(xlUp).Offset(1, 0)

'...  le reste de ton programme

lig = 0

    Next i

merci à vous ca fonctionne comme voulu !
Je note le elseif au lieu du else => if

bonne continuation !

Rechercher des sujets similaires à "erreur syntaxe boucle next"