Delete all rows that match the cell value simultaneously with command button

  • Hello there !!!


    Sorry I am posting all my questions here, just that I find that in this forum people are more responsive :) I have this code that works, but not the way I want it to. I want the loop to delete all the rows that match the cell value with the click of the button. At the moment I have to click through and they delete one by one with each click. Is there a way that it can just go over the work Sheet and delete at the same time ???


    Thank youuu in advance !!!

  • Possibly...

    If I've been helpful, let me know. If I haven't, let me know that too.

  • Re: "Sorry I am posting all my questions here...."

    Why? I am sure everyone here is glad to see you here. Don't be shy. Come back anytime.


    The code from dangelor has you covered and is faster also.


    In your code, if you change this

    Code
    1. i = 1
    2. Do While ws.Cells(i, 3) <> ""

    to this

    Code
    1. For i = LastRow To 1 Step - 1 '<----- When deleting, always start from the end.

    and

    Code
    1. i = i + 1
    2. Loop

    to this

    Code
    1. Next i

    You would have had a home run.


    You could also have the three lines from If to End If in a single line like so

    Code
    1. If ws.Cells(i, 3).Value = student Then ws.Cells(i, 3).EntireRow.Delete


    But stick to dangelor's code as that is the way to go.