EXCEL AUTOMATION & VBA

Delete Blank Rows with VBA (Safe Macro for Any Range)

Exports from banks, accounting systems and web forms often arrive with empty rows between the data. They break filters, sorting, PivotTables and Ctrl+arrow navigation, and deleting them one by one is slow. This free macro finds rows that are completely empty, tells you how many it found, and deletes them only after you click Yes. It never deletes a row that has anything in it, even a single space or a note far to the right.

Excel VBA macro that deleted 9 blank rows from a bank export, kept 40 transactions and a note-only row, and moved the last used row from 52 to 43.
9 blank rows gone, every transaction kept.

Quick answer

  1. Save your workbook as .xlsm (macro-enabled) and keep a backup copy.
  2. Press Alt+F11, choose Insert > Module and paste the code below.
  3. Go back to the sheet. Select a block of rows to limit the macro, or click one cell to check the whole used range.
  4. Press Alt+F8, choose DeleteBlankRows, click Run and confirm with Yes.
Free download: Get the sample bank export (.xlsx) and the macro (delete-blank-rows.txt). The sample has 40 transactions, 9 empty rows and 2 rows that only look empty, plus a Check sheet that confirms nothing was lost. Works in Excel 2010 and later for Windows and Mac. VBA does not run in Excel for the web.
Before you run it: Deleted rows cannot be restored with Ctrl+Z after a macro. Save a copy first. The macro deletes entire sheet rows, so anything else in those rows would go too; that is why it only deletes rows that are empty across the whole sheet.

Step 1: Look at the data

The sample is a bank export with 40 transactions and 9 empty rows. Two more rows only look empty: row 18 holds a single space in column B, and row 27 has a note in column H, off to the right of the data. A quick manual fix (Go To Special > Blanks, then Delete Sheet Rows) is risky here: it deletes every row that has any blank cell, including real transactions with an empty Notes cell (see the Go To Special guide for what it selects). The macro checks the whole row instead.

Excel bank export with 40 transactions and empty rows 6, 12, 14 and 22, a row 18 that holds only a space and a row 27 that holds only a note in column H.
9 empty rows, 2 that only look empty.

Step 2: Paste the macro

Press Alt+F11 to open the VBA editor, select your workbook in the Project pane, choose Insert > Module and paste the full code. There are no settings to change.

Option Explicit

' OneXcel Studio | Delete blank rows (safe version)
' Deletes rows that are completely empty across the whole sheet.
' Checks the selected rows, or the used range when only one cell is selected.
' A row with anything in it (a space, a note in a far column) is kept.

Public Sub DeleteBlankRows()
    Dim ws As Worksheet, rng As Range
    Dim r As Long, firstRow As Long, lastRow As Long, n As Long
    Dim answer As VbMsgBoxResult

    Set ws = ActiveSheet
    If ws.ProtectContents Then
        MsgBox "Unprotect the sheet first (Review > Unprotect Sheet).", vbExclamation
        Exit Sub
    End If

    ' 1. Rows to check: the selection, or the used range of the sheet
    If TypeName(Selection) = "Range" And Selection.Cells.Count > 1 Then
        Set rng = Selection.Areas(1)
    Else
        Set rng = ws.UsedRange
    End If
    firstRow = rng.Row
    lastRow = rng.Row + rng.Rows.Count - 1

    ' 2. Count the rows that are empty across the whole sheet width
    For r = firstRow To lastRow
        If Application.WorksheetFunction.CountA(ws.Rows(r)) = 0 Then n = n + 1
    Next r
    If n = 0 Then
        MsgBox "No blank rows found in rows " & firstRow & " to " & lastRow & ".", vbInformation
        Exit Sub
    End If

    ' 3. Ask once, then delete from the bottom up (so the rows still to check do not move)
    answer = MsgBox("Delete " & n & " blank rows in rows " & firstRow & " to " & lastRow & "?", _
                    vbYesNo + vbQuestion, "Delete blank rows")
    If answer <> vbYes Then Exit Sub

    Application.ScreenUpdating = False
    For r = lastRow To firstRow Step -1
        If Application.WorksheetFunction.CountA(ws.Rows(r)) = 0 Then ws.Rows(r).Delete
    Next r
    Application.ScreenUpdating = True
    MsgBox n & " blank rows deleted.", vbInformation
End Sub

The first part decides which rows to check. If you selected more than one cell, the macro checks only the rows of that selection; otherwise it checks the sheet's used range. It also stops straight away on a protected sheet, where deleting rows would fail halfway.

VBA code that stops on a protected sheet and checks either the selected rows or the used range of the active sheet.
The selection or the used range.

Step 3: How it decides a row is blank

For each row the macro runs COUNTA on the entire sheet row. Only a result of 0 counts as blank. A cell with a space, a formula that returns an empty string, or a note in column XFD all count as content, so those rows stay. If spaces are the reason a row is not deleted, clean them first with remove extra spaces and hidden characters and run the macro again.

VBA loop that counts a row as blank only when COUNTA of the entire sheet row is 0.
COUNTA of the whole row must be 0.

Step 4: Why it deletes from the bottom up

The macro counts first, then asks once: Delete 9 blank rows in rows 1 to 52? Only Yes continues. It then loops from the last row up to the first. Deleting a row moves every row below it up by one; going upwards means the rows still to be checked are above the deleted one and never move. With ScreenUpdating off the screen does not redraw after each deletion, which keeps it fast on a few thousand rows.

VBA code that asks once with a Yes or No message and then deletes the blank rows from the bottom up.
Count, ask, delete upwards.

Step 5: Run it

Select the Import sheet, click A1, press Alt+F8, choose DeleteBlankRows and click Run. The macro shows how many blank rows it found and in which rows. Click Yes to delete them, or No to stop without changing anything.

Excel message box from the macro asking Delete 9 blank rows in rows 1 to 52, with Yes and No buttons.
Nothing changes until you click Yes.

Step 6: Check the result

The 40 transactions now form one block, so filters, sorting and Ctrl+arrow navigation work again. The row with a space (now row 15) and the note row (now row 23) are still there. On the Check sheet, the number of transactions (40) and the total (-20,497.63) are unchanged, the last used row moved from 52 to 43 and the check says Ties.

Excel bank export after the macro with the 40 transactions in one block, the space row now row 15 and the note row now row 23 kept.
One block, nothing lost.
Try it: Run the macro on Import: it asks to delete 9 blank rows; afterwards the last used row is 43 and Check says Ties. Run it again: No blank rows found. On a fresh copy, select A1:H20 first: only the 3 blank rows inside that block are deleted and Check shows 6 blank rows left.

Variations

Troubleshooting

  • Nothing happens: macros are disabled. Save as .xlsm and enable content, or check File > Options > Trust Center.
  • A row that looks empty is not deleted: it has a space, a formula returning an empty string or something far to the right. Press Ctrl+Right Arrow on that row to find it.
  • Unprotect the sheet first: the sheet is protected (Review > Unprotect Sheet).
  • Very slow on 100,000 rows: sort the data so the blank rows go to the bottom, or filter a key column for blanks and delete the visible rows.
  • Want to clear inputs instead of deleting rows? See clear input cells with a macro.

Frequently asked questions

How do I delete blank rows in Excel without VBA?

Filter one column that is always filled for (Blanks), select the visible rows, delete them and clear the filter. Avoid Go To Special > Blanks with Delete Sheet Rows on data that has any empty cells, because it deletes those rows too.

Does the macro delete rows with formulas that return an empty string?

No. COUNTA counts them as content, so the row is kept. That protects formulas you may still need.

Will it delete rows inside an Excel Table?

Yes, if the whole sheet row is empty. A completely empty row inside a Table is deleted and the Table shrinks by one row.

Can I undo it?

Not with Ctrl+Z. Close without saving, or go back to your backup copy.

Does it work on a Mac?

Yes, in Excel for Mac 2016 and later. It does not run in Excel for the web, which does not support VBA.

Final checklist

  • Backup saved, file saved as .xlsm.
  • Block selected, or one cell for the whole used range.
  • Number of rows in the message looks right before you click Yes.
  • Transactions and totals unchanged afterwards.
  • Rows that only look blank cleaned and the macro run again if needed.

Automate the cleanup

Find more ready-to-use Excel templates, learn the formulas in the OneXcel Tutorial Hub, or turn clean data into a report with the OneXcel Dashboard Builder.