VBA supprimer plusieurs lignes si condition
Rep.laba
-
chossette9 Messages postés 6855 Date d'inscription Statut Contributeur Dernière intervention -
chossette9 Messages postés 6855 Date d'inscription Statut Contributeur Dernière intervention -
Bonjour à tous,
Je débute en VBA et j'aurai besoin d'un petit coup de main :
Je possède un tableau contenant environ 1000 lignes et 10 colonnes. Je souhaiterait que si 4 lignes à la suite possède un texte précis dans deux colonnes précises alors ces quatres lignes soient supprimées.
Voilà ce que je fais actuellement pour supprimer deux des quatres lignes en questions, et cela semble fonctionner :
Sub Testaa()
Dim i%
For i = 25000 To 1 Step -1
If Cells(i, 4) = "System OK" And Cells(i, 7) = "phasing out" And Cells(i + 1, 4) = "Wind < start wind" And Cells(i + 1, 7) = "incoming" And Cells(i + 2, 4) = "Wind < start wind" And Cells(i + 2, 7) = "phasing out" And Cells(i + 3, 4) = "System OK" And Cells(i + 3, 7) = "incoming" Then Rows(i).Delete Rows(i + 1).Delete
Next
End Sub
Par contre lorsque je veux le faire pour supprimer les 4 lignes en question cela ne fonctionne plus.
Sub Testaa()
Dim i%
For i = 25000 To 1 Step -1
If Cells(i, 4) = "System OK" And Cells(i, 7) = "phasing out" And Cells(i + 1, 4) = "Wind < start wind" And Cells(i + 1, 7) = "incoming" And Cells(i + 2, 4) = "Wind < start wind" And Cells(i + 2, 7) = "phasing out" And Cells(i + 3, 4) = "System OK" And Cells(i + 3, 7) = "incoming" Then Rows(i).Delete Rows(i + 1).Delete Rows(i + 2).Delete Rows(i + 3).Delete
Next
End Sub
Serait-il impossible de donner plus de deux instructions après le then ?
Merci d'avance de votre aide
Je débute en VBA et j'aurai besoin d'un petit coup de main :
Je possède un tableau contenant environ 1000 lignes et 10 colonnes. Je souhaiterait que si 4 lignes à la suite possède un texte précis dans deux colonnes précises alors ces quatres lignes soient supprimées.
Voilà ce que je fais actuellement pour supprimer deux des quatres lignes en questions, et cela semble fonctionner :
Sub Testaa()
Dim i%
For i = 25000 To 1 Step -1
If Cells(i, 4) = "System OK" And Cells(i, 7) = "phasing out" And Cells(i + 1, 4) = "Wind < start wind" And Cells(i + 1, 7) = "incoming" And Cells(i + 2, 4) = "Wind < start wind" And Cells(i + 2, 7) = "phasing out" And Cells(i + 3, 4) = "System OK" And Cells(i + 3, 7) = "incoming" Then Rows(i).Delete Rows(i + 1).Delete
Next
End Sub
Par contre lorsque je veux le faire pour supprimer les 4 lignes en question cela ne fonctionne plus.
Sub Testaa()
Dim i%
For i = 25000 To 1 Step -1
If Cells(i, 4) = "System OK" And Cells(i, 7) = "phasing out" And Cells(i + 1, 4) = "Wind < start wind" And Cells(i + 1, 7) = "incoming" And Cells(i + 2, 4) = "Wind < start wind" And Cells(i + 2, 7) = "phasing out" And Cells(i + 3, 4) = "System OK" And Cells(i + 3, 7) = "incoming" Then Rows(i).Delete Rows(i + 1).Delete Rows(i + 2).Delete Rows(i + 3).Delete
Next
End Sub
Serait-il impossible de donner plus de deux instructions après le then ?
Merci d'avance de votre aide
A voir également:
- VBA supprimer plusieurs lignes si condition
- Supprimer rond bleu whatsapp - Guide
- Supprimer page word - Guide
- Supprimer pub youtube - Accueil - Streaming
- Fichier impossible à supprimer - Guide
- Supprimer compte instagram - Guide
3 réponses
Bonjour,
si tu veux mettre plusieurs instructions après ton Then, il te faut fermer ton If par un End If.
Cordialement.
si tu veux mettre plusieurs instructions après ton Then, il te faut fermer ton If par un End If.
Cordialement.
Merci de ton aide mais je dois encore faire une erreur, voilà ce que j'ai mis :
Sub Testaa()
Dim i%
For i = 25000 To 1 Step -1
If Cells(i, 4) = "System OK" And Cells(i, 7) = "phasing out" And Cells(i + 1, 4) = "Wind < start wind" And Cells(i + 1, 7) = "incoming" And Cells(i + 2, 4) = "Wind < start wind" And Cells(i + 2, 7) = "phasing out" And Cells(i + 3, 4) = "System OK" And Cells(i + 3, 7) = "incoming" Then Rows(i).Delete Rows(i + 1).Delete Rows(i + 2).Delete Rows(i + 3).Delete End If
Next
End Sub
Vois tu une erreur de syntaxe ?
Merci
Cordialement,
Sub Testaa()
Dim i%
For i = 25000 To 1 Step -1
If Cells(i, 4) = "System OK" And Cells(i, 7) = "phasing out" And Cells(i + 1, 4) = "Wind < start wind" And Cells(i + 1, 7) = "incoming" And Cells(i + 2, 4) = "Wind < start wind" And Cells(i + 2, 7) = "phasing out" And Cells(i + 3, 4) = "System OK" And Cells(i + 3, 7) = "incoming" Then Rows(i).Delete Rows(i + 1).Delete Rows(i + 2).Delete Rows(i + 3).Delete End If
Next
End Sub
Vois tu une erreur de syntaxe ?
Merci
Cordialement,
Je viens de résoudre mon problème, il s'agissait en fait que je n'écrivais pas les instructions sur plusieurs lignes, Voici le code fonctionnant :
Sub Testaa()
Dim i%
For i = 25000 To 1 Step -1
If Cells(i, 4) = "System OK" And Cells(i, 7) = "phasing out" And Cells(i + 1, 4) = "Wind < start wind" And Cells(i + 1, 7) = "incoming" And Cells(i + 2, 4) = "Wind < start wind" And Cells(i + 2, 7) = "phasing out" And Cells(i + 3, 4) = "System OK" And Cells(i + 3, 7) = "incoming" Then
Rows(i + 3).Delete
Rows(i + 2).Delete
Rows(i + 1).Delete
Rows(i).Delete
End If
Next
End Sub
Merci de ton aide
Cordialement,
Sub Testaa()
Dim i%
For i = 25000 To 1 Step -1
If Cells(i, 4) = "System OK" And Cells(i, 7) = "phasing out" And Cells(i + 1, 4) = "Wind < start wind" And Cells(i + 1, 7) = "incoming" And Cells(i + 2, 4) = "Wind < start wind" And Cells(i + 2, 7) = "phasing out" And Cells(i + 3, 4) = "System OK" And Cells(i + 3, 7) = "incoming" Then
Rows(i + 3).Delete
Rows(i + 2).Delete
Rows(i + 1).Delete
Rows(i).Delete
End If
Next
End Sub
Merci de ton aide
Cordialement,