Delete Blank Rows from Excel Worksheet Using Reverse For Next Loop


In the video ‘Automatically cut paste data rows from an Excel worksheet into another‘ we had demonstrated how to transfer data from one worksheet to another. However, once the data is transferred we are left with the original worksheet with single and multiple blank rows between the data. Using a normal ‘for next’ looping process in VBA we can remove the single blank rows between the data. But the multiple blank rows are not removed completely and blank rows may still remain after running the loop as shown in the image below:

Using Normal For Next Loop to Delete Blank Rows in Excel Worksheet

Using Normal For Next Loop to Delete Blank Rows in Excel Worksheet

In order to delete all the blank rows between the data in the Excel worksheet we have to use ‘reverse’ looping process as shown in the training video below:

 

The complete VBA code:

Normal ‘for next’ loop:
Sub delete_blank_rows_normal()
Dim i As Long, LastRow As Long
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To LastRow
MsgBox “You are now in row ” & i
If Cells(i, 1) = “” Then
Cells(i, “A”).EntireRow.Delete
End If
Next

End Sub

Reverse ‘for next’ looping VBA code:
Sub delete_blank_rows()
Dim i As Long, LastRow As Long
LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = LastRow To 2 Step -1
MsgBox “You are now in row ” & i
If Cells(i, 1) = “” Then
Cells(i, “A”).EntireRow.Delete
End If
Next

End Sub


One thought on “Delete Blank Rows from Excel Worksheet Using Reverse For Next Loop

  1. Aires

    Hi, as I’m not familiar with macro’s in Excel, can someone perhaps help me out with the following?

    I have a sheet with in cel
    A2: the name of the person who entered a specific problem
    B2: the date on which sthis person entered a problem.
    C2: a short description of the problem.
    D2: the status of the problem.

    What I would like Excel to do automatically is: cut and past the entire row to another sheet when the value in cell D2 is changed into “Solved” and auto delete the empty row that remains in the first sheet.

    Sheet 1 contains not one but multiple rows, each with a problem inserted. And the action needs to apply for all rows.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *