For avec 2 conditions if

Bonjour à tous,
J’ai une macro qui ouvre des fichiers et supprime, soit la 2è ou la 3è feuille de chaque fichier enregistré dans un répertoire “J9”.

Avant la macro fonctionnait bien avec la 1è condition. Lorsque j’ai ajouté la 2è condition (VBA ci-dessous) la macro a cessé de fonctionner. (ci-joint une capture d’écran du message d’erreur).
J’ai essayé plusieurs exemple de VBA, mais sans succès.

Si vous me permettez, je vous transmets le VBA en question.
En vous souhaitant une bonne réception.
Cher internaute, je vous remercie à l’avance pour votre aide :wink:

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


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

For i = 10 To lastrow

'1ère condition:
If Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") = "RESEAU" 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
                Workbooks(wb2).Save
                    Workbooks(wb2).Close
      
'2ème Condition: (que j'ai ajouté)
ElseIf Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") <> "RESEAU" 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
                Workbooks(wb2).Save
                     Workbooks(wb2).Close
      
   End If
  Next objFile
End If

Next i

nombre = WorksheetFunction.CountIf(Range("G:G"), "OUI")
   MsgBox ("TERMINÉ : " & nombre & " ONGLETS EFFECES")

End Sub

Bonjour,

Normal, tu boucle 2 fois sur le même lien sans fermer la boucle

Teste comme ceci

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
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  chemin = Workbooks(Wb).Sheets("PARAMETRES").Cells(9, "J")
  Set objFolder = objFSO.GetFolder(chemin)
    For i = 10 To lastrow
'1ère condition:
      If Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") = "RESEAU" 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
            Workbooks(wb2).Save
            Workbooks(wb2).Close
'2ème Condition: (que j'ai ajouté)
          ElseIf Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "G") = "OUI" And Workbooks(Wb).Sheets("PARAMETRES").Cells(i, "A") <> "RESEAU" Then
            Workbooks(wb2).Sheets(2).Delete
            Workbooks(wb2).Save
            Workbooks(wb2).Close
          End If
        Next objFile
      End If
    Next i
  nombre = WorksheetFunction.CountIf(Range("G:G"), "OUI")
  MsgBox ("TERMINÉ : " & nombre & " ONGLETS EFFECES")
End Sub

J’ai testé ça n’a pas marché.

Re,

Sans classeur modèle, en plus sur du Word, je ne peut pas tout résoudre

Problème résolu.
Merci à tous.

Ce sujet a été automatiquement fermé après 30 jours. Aucune réponse n’est permise dorénavant.