You can use this...
Sub ClearEmptyRows()
Dim r As Long, lastrow As Long, WS As Worksheet, killRng As Range
Set WS = ActiveSheet
lastrow = WS.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Set killRng = WS.Rows(Rows.Count)
For r = 1 To lastrow
If Application.WorksheetFunction.CountA(WS.Rows(r)) = 0 Then
Set killRng = Union(killRng, WS.Rows(r))
End If
Next r
killRng.Delete
End Sub
A couple comments on this code for newbies as it's a common routine to loop through rows and do something (delete, highlight, hide, etc.)
- It's always best to interact with Worksheet as infrequently as possible. Thus we execute the
Delete
AFTER all of the rows have been identified.
- You can't
Union
an empty range, so I set the killRng
to initially be the entire last row (hopefully this is always blank), and then the macro can proceed with Union
. One could get around this by including an if-statement, but this requires the macro check if range exists on each row.