VBA macros · Data

VBA macro to delete blank rows in Excel

Delete every completely empty row on the active sheet in one go. The macro collects the rows first and deletes them in a single fast step.

Windows + Mac

Exports from accounting systems and copied web tables often come with empty rows scattered through the data. Empty rows break sorting, filters, pivot tables and Ctrl+Arrow navigation, because Excel treats them as the end of the data. This macro finds every row in the used range that is completely empty and deletes them all at once.

VBA
Option Explicit

Sub DeleteBlankRows()
    Dim ws As Worksheet
    Dim firstRow As Long, lastRow As Long, r As Long
    Dim toDelete As Range
    Dim n As Long

    Set ws = ActiveSheet
    With ws.UsedRange
        firstRow = .Row
        lastRow = .Row + .Rows.Count - 1
    End With

    Application.ScreenUpdating = False

    For r = firstRow To lastRow
        ' CountA = 0 means nothing at all in the row
        If Application.WorksheetFunction.CountA(ws.Rows(r)) = 0 Then
            If toDelete Is Nothing Then
                Set toDelete = ws.Rows(r)
            Else
                Set toDelete = Union(toDelete, ws.Rows(r))
            End If
            n = n + 1
        End If
    Next r

    If Not toDelete Is Nothing Then toDelete.Delete   ' one delete for all rows

    Application.ScreenUpdating = True
    MsgBox n & " blank row(s) deleted.", vbInformation
End Sub
Delete all empty rows on the active sheet

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick DeleteBlankRows and click Run.
  5. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

The used range tells the macro where the data starts and ends, so it does not scan a million empty rows. For each row in that range, WorksheetFunction.CountA counts non-empty cells, exactly like the COUNTA function on a sheet. A result of zero means the row is completely empty.

Instead of deleting rows one at a time, the macro adds each empty row to a single range with Union. Deleting inside the loop would be slow (every deletion shifts all rows below it and recalculates) and would also require looping from the bottom up so the row numbers do not shift under you. Collecting first and deleting once avoids both problems and is many times faster on large sheets.

toDelete starts as Nothing, so the first empty row is assigned directly and later rows are added with Union. This is a standard pattern; see Range and Cells: working with cells in VBA and Make your macros fast: speed optimization in VBA.

Change it

Often a row is practically empty when one key column is empty, even if a stray note sits in another column. To delete rows where column A is blank, replace the CountA test with If Len(ws.Cells(r, "A").Value) = 0 Then.

To work only on the selected rows instead of the whole sheet, set the bounds from the selection:

VBA
    With Selection
        firstRow = .Row
        lastRow = .Row + .Rows.Count - 1
    End With
Limit the macro to the selected rows

The same idea works for columns: loop over ws.Columns(c) and collect empty columns with Union. To delete rows that contain a particular value instead of nothing, see VBA macro to delete rows based on a cell value in Excel.

What can go wrong

Watch out. A macro cannot be undone with Ctrl+Z (Mac: Cmd+Z). Save the workbook, or make a copy with VBA macro to save a backup copy with a timestamp in Excel, before you run it on real data.

A formula that returns an empty string (="") is not blank to CountA, so rows full of such formulas stay. Cells containing only a space are not blank either; clean them first with VBA macro to trim spaces in selected cells in Excel. Rows inside an Excel table (ListObject) are deleted as table rows, which is fine, but if the selection crosses the table edge you may see This operation is not allowed. Convert the table to a range or delete inside the table only.

Tip. Works the same on Windows and Mac. Without VBA you can do the same with Home › Find & Select › Go To Special › Blanks on a key column, then Delete › Delete Sheet Rows, but that deletes rows with any blank cell in the selection, not only fully empty rows.

Related: VBA macro to delete rows based on a cell value in Excel, VBA macro to reset the used range and clear excess formatting, Find the last row and build dynamic ranges in VBA.

More vba macros