Boucle avec suppression de ligne

robindiluffy -  
 robindiluffy -
Bonjour,





Je dois effectuer le tri d'un tableau excel tiré d'un extract, mais je ne comprend pas il me semble que mon programme ne comporte pas d'erreur mais malheureusement lorsque je compile le programme s'exécute sans jamais s'arrêter.
Option Explicit

Sub tri()
Dim FL2 As Worksheet, Cell As Range, NoCol As Integer
Dim NoLig As Long, DerLig As Long, Val1 As Variant
Dim cell_suiv1 As Variant, Val2 As Integer
Dim Val3 As Integer, cell_suiv2 As Integer, cell_suiv3 As Integer

Set FL2 = Worksheets("Feuil2")


DerLig = FL2.Range("A" & Rows.Count).End(xlUp).Row

NoCol = 5


For NoLig = 2 To DerLig
Val1 = FL2.Cells(NoLig, NoCol)
cell_suiv1 = FL2.Cells(NoLig + 1, NoCol)
If Val1=cell_suiv1 Then
Val2 = FL2.Cells(NoLig, NoCol + 1)
cell_suiv2 = FL2.Cells(NoLig + 1, NoCol + 1)
If Val2 = cell_suiv2 Then
Val3 = FL2.Cells(NoLig, NoCol + 8)
cell_suiv3 = FL2.Cells(NoLig + 1, NoCol + 8)
If Val3 = cell_suiv3 Then
FL2.Cells(NoLig, NoCol +8).EntireRow.Delete
NoLig = NoLig - 1
End If
End If
ElseIf IsEmpty(Val1) Then
FL2.Cells(NoLig, NoCol).EntireRow.Delete
NoLig = NoLig - 1
End If
Next NoLig

End Sub

Est-ce que l'un d'entre vous peut me dire si mon code est juste ? merci d'avance
A voir également:

1 réponse

ThauTheme Messages postés 1442 Date d'inscription   Statut Membre Dernière intervention   160
 
Bonjour Robindiluffy

Je n'ai pas testé ton code mais en principe, lorsque l'on boucle en supprimant une ligne il faut commencer par la dernière ligne en remontant :
For NoLig = DerLig To 2 Step -1
'...
Next NoLig


0
robindiluffy
 
Merci pour ta réponse rapide.

En effet j'ai continué de farfouiller sur le net et j'ai remarqué qu'il fallait que je tente du bas vers le haut et ca fonctionne!
Parfait merci
0