VBA (Excel) - Delete a row

Solved
XRay -  
dlescot Posted messages 1 Status Member -
Hello!

I would like to know how to search for a value in a column and delete the corresponding row...
Here is what I did:

 Range("A1").Select Do While ActiveCell <> "toto" ActiveCell.Offset(1, 0).Select Loop Selection.Delete Shift:=xlUp


Actually, I can't select the entire row to delete it...

Does anyone have a solution?... Thank you in advance! ;o)

14 answers

  1. Mangda
     
    And simply a
    Selection.EntireRow.Delete

    Wouldn't that be easier?
    Or a bit more complicated, but simpler than what was proposed:
    Rows(Selection.Row).Delete shift:=xlUp


    But I see at least two problems with your code:
    • If "toto" appears twice, it will only be deleted once
    • If "toto" never appears, you will get an error on the offset at line 65536.


    Not knowing how many rows you have, you could use the following code:
    For i = Cells(1, 1).CurrentRegion.Rows.Count To 1 Step -1 If Cells(i, 1).Value = "toto" Then Cells(i, 1).EntireRow.Delete Next

    knowing that you can replace Cells(1, 1).CurrentRegion.Rows.Count with the corresponding number of rows.
    73
    1. fyj
       
      You are super strong!
      0
    2. Nhan
       
      Thank you, Mangda.
      0
    3. Annalord
       
      Annalord
      I have a similar problem
      I copied tutorials from the web using Open Office but I can't delete the lines without removing the text
      Maybe someone has a tip
      Thanks in advance
      0
    4. YOUHA
       
      Sub delete_rows() ThisWorkbook.Worksheets("Sheet3").Activate j = 1 For i = 1 To Range("C65536").End(xlUp).Row 'calculates the last filled cell in column 3 column C If Cells(j, 3).Value = "no failure" Then Cells(j, 3).EntireRow.Delete Else j = j + 1 End If Next i MsgBox "Macro completed" End Sub
      0