Problème VBA boucle pour supprimer ligne

Rafilo13

XLDnaute Junior
Bonjour,

J'essaye de construire un macro qui me permets de supprimer les lignes quand la valeur d'une cellule est egale X1 dans ma colonne G

Voici ce que j'ai essayé de faire, mais je ne sais pas comment exprimer le fait que je veux supprimer la ligne de la cellule testé :

Sheets("feuil1").Activate

Dim z As Range

For Each z In Range("G2:G" & Cells(Rows.Count, 1).End(xlUp).Row)

If z.Value = "X1" Then
Row.Select
Selection.Delete shift:=xlUp

End If
Next z

End Sub

Merci par avance et bonne journée a tous.
 
G

Guest

Guest
Re : Problème VBA boucle pour supprimer ligne

Bonjour,

Pour supprimer des lignes, il faut partir du bas vers le haut.

Code:
Sub t()
    Dim z As Long
    'Partir du bas du tableau
    For z = Range("G" & Rows.Count).End(xlUp).Row To 2 Step -1
        If Range("G" & z) = "X1" Then Range("G" & z).EntireRow.Delete xlShiftUp
    Next z
End Sub

A+
 
Dernière modification par un modérateur:

flyonets44

XLDnaute Occasionnel
Re : Problème VBA boucle pour supprimer ligne

Bonjour
tu pourras t'inspirer de ce code
Sub DeleteRows
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim Findstring as string, RngB as range, Lr&
lr = Cells(Rows.Count, "A").End(xlUp).Row
FindString = "X1"
Set Rngb = Range("X:X" & lastrow).Find(what:=FindString, lookat:=xlWhole)
While Not (rngb Is Nothing)
rngb.entirerow.Delete
Set rngb = Range("A:A" & lastrow).Find(what:=FindString, lookat:=xlWhole)
Wend
set rngB=nothing
Application.Calculation = xlCalculationAutomatic
End Sub
 

Rafilo13

XLDnaute Junior
Re : Problème VBA boucle pour supprimer ligne

Bonjour,

Merci a vous deux celle Hasco fonctionne au top pour mon cas, je garde quand même la tienne au chaud flyonets pour de futur projet.

En tout cas merci encore a vous deux et bonne journée
 

Pierrot93

XLDnaute Barbatruc
Re : Problème VBA boucle pour supprimer ligne

Bonjour à tous,

si l'on veut utiliser la méthode "find", autant la combiner avec la méthode "FindNext" étudiée pour, enfin me semble t-il :
Code:
Dim c As Range
With Sheets("feuil1").Columns(7)
    Set c = .Find("X1", , xlValues, xlWhole, , , False)
    If Not c Is Nothing Then
        Do
            c.EntireRow.Delete
            Set c = .FindNext
        Loop While Not c Is Nothing
    End If
End With

bon après midi
@+
 

Statistiques des forums

Discussions
312 484
Messages
2 088 794
Membres
103 969
dernier inscrit
fabien03