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.

Quick answer
- Save your workbook as .xlsm (macro-enabled) and keep a backup copy.
- Press Alt+F11, choose Insert > Module and paste the code below.
- Go back to the sheet. Select a block of rows to limit the macro, or click one cell to check the whole used range.
- Press Alt+F8, choose DeleteBlankRows, click Run and confirm with Yes.
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.

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.

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.

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.

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.

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.

Variations
- Only a block: select the rows first (for example a table in the middle of a sheet). The macro still checks the whole width of each row, so data beside the block is safe.
- Blank cells, not rows: to fill gaps in a column instead of deleting rows, see fill blank cells without overwriting data.
- After deleting, remove repeated records with remove duplicate rows safely.
- Cleaning many files? Combine them first with combine multiple workbooks with VBA, then run this once. To share only what is visible, see copy visible cells only.
- Need one sheet per region afterwards? Use the split data into multiple sheets macro; empty rows would show up as rows with no Region and make its Split Log say Check, so delete them first.
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.