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