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


Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

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

  1. 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.

Leave a Reply

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