Empty rows can clutter your Excel worksheet and make your data harder to work with. Whether you’re cleaning up survey responses, exporting reports from external systems, or just maintaining tidy tables, removing those blank rows can save time and reduce errors.
In this article, youโll learn four practical ways to delete empty rows in Excel using VBA. From selecting a range manually to scanning the whole sheet automatically, these methods help you simplify your data cleanup process. Pick the one that best fits your scenario, and apply it with just a few lines of code.
Steps to delete empty rows inside a user-selected range using VBA:
โค In Excel, manually highlight the rectangular block that might contain blank rows.
โค Press Alt + F11 , go to Insert >> Module, and paste the code below:
Sub DeleteBlankRowsInSelection()
ย ย ย Dim rng As Range
ย ย ย Dim rowRange As Range
ย ย ย ' Use the current selection as the target range
ย ย ย Set rng = Selection
ย ย ย If rng Is Nothing Then
ย ย ย ย ย ย ย MsgBox "No range selected.", vbExclamation
ย ย ย ย ย ย ย Exit Sub
ย ย ย End If
ย ย ย ' Loop through each row in the selection (bottom โ top prevents skips)
ย ย ย For Each rowRange In rng.Rows
ย ย ย ย ย ย ย If Application.WorksheetFunction.CountA(rowRange) = 0 Then
ย ย ย ย ย ย ย ย ย ย ย rowRange.EntireRow.Delete
ย ย ย ย ย ย ย End If
ย ย ย Next rowRange
ย ย ย MsgBox "Empty rows removed from " & rng.Address(False, False) & ".", vbInformation
End Sub
โค The macro targets only the rows inside your selected block, outside data stays untouched.
โค It uses CountA to check for completely blank rows, and deletes them from bottom to top.
โค This prevents skipping rows during deletion caused by shifting cells upward.
โค After running, a message box confirms the range cleaned and removal success.
โค To execute: go back to Excel, press Alt + F8 , choose DeleteBlankRowsInSelection, and click Run.

Delete Empty Rows in a UserโSelected Range with VBA
Cleaning up a pasted block or partial dataset often means removing completely blank rows without disturbing data elsewhere. VBA can target only the range you highlight and instantly delete every fully empty row inside that block.
For this demonstration, our sample data on Sheet1 spans columnsโฏAโD and includes several blank rows between records. The macro below deletes those empty rows only within the range you select, leaving the rest of the sheet untouched.

Steps:
โค Drag your mouse to highlight the range that you want to delete blank rows from.

โค Press Alt + F11 , choose Insertโฏ>>โฏModule.

โค Paste this code:
Sub DeleteBlankRowsInSelection()
ย ย ย Dim rng As Range
ย ย ย Dim rowRange As Range
ย ย ย ' Use the current selection as the target range
ย ย ย Set rng = Selection
ย ย ย If rng Is Nothing Then
ย ย ย ย ย ย ย MsgBox "No range selected.", vbExclamation
ย ย ย ย ย ย ย Exit Sub
ย ย ย End If
ย ย ย ' Loop through each row in the selection (bottom โ top prevents skips)
ย ย ย For Each rowRange In rng.Rows
ย ย ย ย ย ย ย If Application.WorksheetFunction.CountA(rowRange) = 0 Then
ย ย ย ย ย ย ย ย ย ย ย rowRange.EntireRow.Delete
ย ย ย ย ย ย ย End If
ย ย ย Next rowRange
ย ย ย MsgBox "Empty rows removed from " & rng.Address(False, False) & ".", vbInformation
End Sub

โง CountA(rowRange) counts nonโblank cells in each row; a result ofโฏ0 means the row is empty.
โง rowRange.EntireRow.Delete removes every blank row found, and looping bottomโtoโtop prevents missed rows as Excel reindexes.
โง A message box confirms the address of the cleaned range and that blank rows are gone.
โค Return to Excel, keep your desired range selected, press Alt + F8 , choose DeleteBlankRowsInSelection, and click Run.

โค All completely blank rows inside the highlighted block disappear, leaving a contiguous dataset.
Automatically Delete Empty Rows in a Defined Range
Blank rows buried inside a data block can break sorting, totals, or exports. VBA can clean those rows from a fixed range you hardโcode in the macro and slide the remaining data upward, leaving a clean dataset.
For this demonstration, the range A2:D14 on Sheet1 contains a few fully blank rows. The macro below deletes every completely empty row within that block and shifts the rows below upward so the table has no gaps.
Steps:
โค Press Alt + F11 , choose Insertโฏ>>โฏModule, and paste the code:
Sub TidyRange_DeleteBlankRows()
ย ย ย Dim ws As Worksheet
ย ย ย Dim targetRange As Range
ย ย ย Dim r As Long, firstRow As Long, lastRow As Long
ย ย ย Dim colCount As Long
ย ย ย Set ws = ThisWorkbook.Sheets("Sheet1")ย ย ย ย ย ' change sheet name if needed
ย ย ย Set targetRange = ws.Range("A2:D14")ย ย ย ย ย ย ย ' <- define the block to tidy
ย ย ย firstRow = targetRange.Rows(1).Row
ย ย ย lastRowย = targetRange.Rows(targetRange.Rows.Count).Row
ย ย ย colCount = targetRange.Columns.Count
ย ย ย ' Loop upward through rows to avoid skipping after deletions
ย ย ย For r = lastRow To firstRow Step -1
ย ย ย ย ย ย ย If Application.WorksheetFunction.CountA( _
ย ย ย ย ย ย ย ย ย ย ย ย ws.Cells(r, targetRange.Column).Resize(1, colCount)) = 0 Then
ย ย ย ย ย ย ย ย ย ย ย ws.Rows(r).Delete
ย ย ย ย ย ย ย End If
ย ย ย Next r
ย ย ย MsgBox "Blank rows removed from " & targetRange.Address(False, False) & _
ย ย ย ย ย ย ย ย ย ย "; data shifted up.", vbInformation
End Sub

โง firstRow, lastRow, and colCount capture the blockโs boundaries.
โง The loop scans rows bottomโtoโtop so row indices donโt shift after each deletion.
โง CountA on the row slice (Resize(1, colCount)) checks whether every cell is blank; if so, that entire sheet row is deleted and rows below move up.
โง A popโup confirms the blockโs address and that all blank rows have been removed.
โค Return to Excel, press Alt + F8 , select TidyRange_DeleteBlankRows, and click Run.

โค The macro deletes every completely blank row inside A2:D14 and closes all gaps, leaving a contiguous dataset.
Remove Every Blank Row from the Worksheet in a Single Run
Blank lines scattered throughout a worksheet can inflate file size and disrupt sorting or formulas. VBA can scan the whole sheet in one pass, remove every row that is completely empty, and leave your data compact from top to bottom.
For this demonstration, the code targets the worksheet without the need for any range selection or setup, the macro simply deletes each entirely blank row it finds.
Steps:
โค Press Alt + F11 , choose Insertโฏ>>โฏModule, and paste this code:
Sub DeleteAllBlankRows_OnSheet()
ย ย ย Dim ws As Worksheet
ย ย ย Dim lastRow As Long
ย ย ย Dim r As Long
ย ย ย ' Set the worksheet to clean
ย ย ย Set ws = ThisWorkbook.Sheets("Sheet1")ย ย ' change if needed
ย ย ย ' Find last used row in column A (safest anchor)
ย ย ย lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
ย ย ย ' Loop upward through every sheet row
ย ย ย For r = lastRow To 2 Step -1ย ย ย ย ย ย ย ย ย ย ย ย ' skip header row 1
ย ย ย ย ย ย ย If Application.WorksheetFunction.CountA(ws.Rows(r)) = 0 Then
ย ย ย ย ย ย ย ย ย ย ย ws.Rows(r).Delete
ย ย ย ย ย ย ย End If
ย ย ย Next r
ย ย ย MsgBox "All completely blank rows removed from " & ws.Name & ".", _
ย ย ย ย ย ย ย ย ย ย vbInformation
End Sub

โง lastRow finds the bottom data row (using columnโฏA as an anchor).
โง The loop runs bottomโtoโtop; deleting rows from the bottom prevents index skips.
โง CountA(ws.Rows(r)) counts nonโblank cells across the entire row; a result ofโฏ0 means the row is empty and should be deleted.
โง Each qualifying row is removed, and a message box confirms completion.
โค Return to Excel, press Alt โฏ+โฏ F8 , select DeleteAllBlankRows_OnSheet, and click Run.

โค Every completely blank row on Sheet1 is deleted in one shot, leaving a gapโfree worksheet.
Downloadable Resources
Get our practice workbook or necessary files that we have worked with to prepare this article.
Delete Empty Rows with VBA.xlsx
Frequently Asked Questions
Whatโs the fastest VBA way to remove blank rows?
Use Columns(“A:D”).SpecialCells(xlCellTypeBlanks).EntireRow.Delete. It instantly selects all blank cells in the target columns and deletes their rows,fast for large, scattered blanks.
Why loop bottomโtoโtop when deleting rows in VBA?
Deleting from the bottom prevents row reโindexing issues. When rows shift up, your counter stays correct, ensuring no rows are skipped or doubleโchecked during the loop.
How can I delete rows only if an entire row is blank?
Apply WorksheetFunction.CountA(rng) = 0 on each row. CountA counts nonโblank cells, so a zero result guarantees every column in that row is empty before deletion.
Can I undo a VBA rowโdeletion macro?
Yes, if you run the macro manually, Excel treats it as one action, so Ctrl โฏ+โฏ Z restores deleted rows. Automated macros in loops, however, may break undo history.
Wrapping Up
Deleting empty rows in Excel using VBA helps keep your data clean, compact, and analysis-ready. Whether you’re targeting a selected range, a defined block, or the entire worksheet, these methods offer reliable solutions for different cleanup needs. Choose the one that fits your workflow, run the macro, and instantly remove clutter. With just a few lines of code, you can maintain structured, gap-free datasets and make your spreadsheets easier to manage.
