Delete a row in Excel VBA

Chris Nielsen’s solution is simple and will work well. A slightly shorter option would be…

ws.Rows(Rand).Delete

…note there is no need to specify a Shift when deleting a row as, by definition, it’s not possible to shift left

Incidentally, my preferred method for deleting rows is to use…

ws.Rows(Rand) = ""

…in the initial loop. I then use a Sort function to push these rows to the bottom of the data. The main reason for this is because deleting single rows can be a very slow procedure (if you are deleting >100). It also ensures nothing gets missed as per Robert Ilbrink’s comment

You can learn the code for sorting by recording a macro and reducing the code as demonstrated in this expert Excel video. I have a suspicion that the neatest method (Range(“A1:Z10”).Sort Key1:=Range(“A1”), Order1:=xlSortAscending/Descending, Header:=xlYes/No) can only be discovered on pre-2007 versions of Excel…but you can always reduce the 2007/2010 equivalent code

Couple more points…if your list is not already sorted by a column and you wish to retain the order, you can stick the row number ‘Rand’ in a spare column to the right of each row as you loop through. You would then sort by that comment and eliminate it

If your data rows contain formatting, you may wish to find the end of the new data range and delete the rows that you cleared earlier. That’s to keep the file size down. Note that a single large delete at the end of the procedure will not impair your code’s performance in the same way that deleting single rows does

Leave a Comment