Erreur: Next sans For

Bonjour à tous,

J'ai rajouté une 3è condition dans une macro, mais ça bloque au niveau de "Next i"

Message d'erreur: Next sans For

Est ce que vous avez une idée de ce qu'il faut ajouter s'il vous plait ?

Je vous remercie pour votre aide.

Sub DELETE_SHEETS()

lastrow = Range("G" & Rows.Count).End(xlUp).Row
Wb = ThisWorkbook.Name
Dim objFolder As Object
Dim objFile As Object
Dim chemin As String

Dim nombre As Double: nombre = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
chemin = Workbooks(Wb).Sheets("PARAMETRES").Cells(9, "J")
Set objFolder = objFSO.GetFolder(chemin)

For i = 10 To lastrow

'1er CONDITION:
If Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") = "AP" Then

  For Each objFile In objFolder.Files

   If InStr(objFile.Name, Sheets("PARAMETRES").Cells(i, "B")) <> 0 Then
     LienFichier = chemin & "\" & objFile.Name
     ActiveWorkbook.FollowHyperlink Address:=(LienFichier)
     wb2 = objFile.Name

      Application.DisplayAlerts = False

            Workbooks(wb2).Sheets(2).Delete 'SUPP CDRM ABR
            Workbooks(wb2).Sheets(3).Delete 'SUPP CDRM Autres métiers ABR
                Workbooks(wb2).Save
                    Workbooks(wb2).Close

       nombre = nombre + 1

      End If
    Next objFile

'2è CONDITION:
If Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") = "ABR" Then

   For Each objFile In objFolder.Files

   If InStr(objFile.Name, Sheets("PARAMETRES").Cells(i, "B")) <> 0 Then
     LienFichier = chemin & "\" & objFile.Name
     ActiveWorkbook.FollowHyperlink Address:=(LienFichier)
     wb2 = objFile.Name

      Application.DisplayAlerts = False
            Workbooks(wb2).Sheets(3).Delete 'SUPP CDRM AP
            Workbooks(wb2).Sheets(3).Delete 'SUPP CDRM Autres métiers ABR
                Workbooks(wb2).Save
                     Workbooks(wb2).Close

       nombre = nombre + 1

      End If
    Next objFile

'3è CONDITION:
ElseIf Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") = "AUTRE_ABR" Then

   For Each objFile In objFolder.Files

   If InStr(objFile.Name, Sheets("PARAMETRES").Cells(i, "B")) <> 0 Then
     LienFichier = chemin & "\" & objFile.Name
     ActiveWorkbook.FollowHyperlink Address:=(LienFichier)
     wb2 = objFile.Name

      Application.DisplayAlerts = False
            Workbooks(wb2).Sheets(2).Delete 'SUPP CDRM ABR
            Workbooks(wb2).Sheets(2).Delete 'SUPP CDRM AP
                Workbooks(wb2).Save
                     Workbooks(wb2).Close

       nombre = nombre + 1

      End If
    Next objFile

  End If
Next i 'ici Message d'erreur : Next sans For

   MsgBox ("TERMINÉ : " & nombre & " ONGLETS EFFECES")

End Sub

bonjour,

il te manque un end if avant le next i

Bonjour Las-dias, bonjour le forum,

Un mélange de If et de ElseIf et une mauvaise d'identation ! Du coup on est un peu perdu...

Peut-être comme ça :

Sub DELETE_SHEETS()
Dim objFolder As Object
Dim objFile As Object
Dim chemin As String
Dim nombre As Double: nombre = 0

lastrow = Range("G" & Rows.Count).End(xlUp).Row
Wb = ThisWorkbook.Name
Set objFSO = CreateObject("Scripting.FileSystemObject")
chemin = Workbooks(Wb).Sheets("PARAMETRES").Cells(9, "J")
Set objFolder = objFSO.GetFolder(chemin)

For i = 10 To lastrow
    '1er CONDITION:
    If Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") = "AP" Then
        For Each objFile In objFolder.Files
            If InStr(objFile.Name, Sheets("PARAMETRES").Cells(i, "B")) <> 0 Then
                LienFichier = chemin & "\" & objFile.Name
                ActiveWorkbook.FollowHyperlink Address:=(LienFichier)
                wb2 = objFile.Name
                Application.DisplayAlerts = False
                Workbooks(wb2).Sheets(2).Delete 'SUPP CDRM ABR
                Workbooks(wb2).Sheets(3).Delete 'SUPP CDRM Autres métiers ABR
                Workbooks(wb2).Save
                Workbooks(wb2).Close
                nombre = nombre + 1
            End If
        Next objFile
    End If
    '2è CONDITION:
    If Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") = "ABR" Then
        For Each objFile In objFolder.Files
            If InStr(objFile.Name, Sheets("PARAMETRES").Cells(i, "B")) <> 0 Then
                LienFichier = chemin & "\" & objFile.Name
                ActiveWorkbook.FollowHyperlink Address:=(LienFichier)
                wb2 = objFile.Name
                Application.DisplayAlerts = False
                Workbooks(wb2).Sheets(3).Delete 'SUPP CDRM AP
                Workbooks(wb2).Sheets(3).Delete 'SUPP CDRM Autres métiers ABR
                Workbooks(wb2).Save
                Workbooks(wb2).Close
                nombre = nombre + 1
            Next objFile
    End If
    '3è CONDITION:
    If Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") = "AUTRE_ABR" Then
        For Each objFile In objFolder.Files
            If InStr(objFile.Name, Sheets("PARAMETRES").Cells(i, "B")) <> 0 Then
                LienFichier = chemin & "\" & objFile.Name
                ActiveWorkbook.FollowHyperlink Address:=(LienFichier)
                wb2 = objFile.Name
                Application.DisplayAlerts = False
                Workbooks(wb2).Sheets(2).Delete 'SUPP CDRM ABR
                Workbooks(wb2).Sheets(2).Delete 'SUPP CDRM AP
                Workbooks(wb2).Save
                Workbooks(wb2).Close
                nombre = nombre + 1
            End If
        Next objFile
    End If
Next i 'ici Message d'erreur : Next sans For
MsgBox ("TERMINÉ : " & nombre & " ONGLETS EFFECES")
End Sub

J'ai mis ce que vous avez modifié, ça m'affiche encore un message d'erreur, cette fois-ci au niveau de "Next objFile" de la 2è condition.

ci-joint une capture d’écran.

sans titre

Bonsoir,

Problème résolu.

      End If
    Next objFile   
  End If
  'dans chaque condition.
  

Je vous remercie !

Rechercher des sujets similaires à "erreur next"