You can delete blank rows in Excel with a short VBA macro that loops through your used range from the bottom up and calls EntireRow.Delete on any row where every cell in the used columns is empty. The bottom-up loop is the key detail: deleting rows from top to bottom shifts the rows beneath upward, so the loop skips rows and leaves blanks behind. Working from the last row back to the first avoids that problem entirely. The steps below give you a tested pattern, explain how to limit it to your actual data columns so rows that are only partly blank are not wiped, and cover what to do when the macro deletes too much, too little, or runs too slowly.
What You'll Need
- Excel on Windows or Mac with macros enabled (the Developer tab visible, or access to the Visual Basic Editor via Alt+F11 on Windows, or the Tools menu on Mac).
- A backup copy of the workbook. Macros that delete rows cannot be undone with Ctrl+Z, so save a copy before you run anything.
- An understanding of which columns define "blank" for your data. A row is usually only truly blank if every relevant column is empty, not just column A.
- Basic comfort with the Visual Basic Editor: inserting a module, pasting code, and pressing F5 or using Run > Run Sub.
Step-by-Step
- Save a copy of the workbook under a new name, such as Data_backup.xlsm. Keep the original untouched until you are happy with the result.
- Save the workbook as a macro-enabled file if it is not already. Go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Code in a normal .xlsx file will be lost when you close it.
- Open the Visual Basic Editor. On Windows press Alt+F11. On Mac, use Tools > Macro > Visual Basic Editor (menu wording can differ slightly by version). If the Developer tab is not visible, enable it via File > Options > Customize Ribbon, or Excel > Settings on Mac.
- Insert a module. In the editor, right-click your workbook name in the Project Explorer and choose Insert > Module. A blank code window appears.
- Paste the macro. Use this code, which checks columns A to the last used column and deletes a row only when every one of those cells is empty:
Sub DeleteBlankRows() Dim ws As Worksheet Dim lastRow As Long, lastCol As Long Dim r As Long, c As Long Dim isBlank As Boolean Set ws = ActiveSheet lastRow = ws.Cells.Find(What:="*", After:=ws.Range("A1"), _ LookIn:=xlFormulas, LookAt:=xlPart, _ SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row lastCol = ws.Cells.Find(What:="*", After:=ws.Range("A1"), _ LookIn:=xlFormulas, LookAt:=xlPart, _ SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column Application.ScreenUpdating = False For r = lastRow To 1 Step -1 isBlank = True For c = 1 To lastCol If Len(Trim(ws.Cells(r, c).Value)) > 0 Then isBlank = False Exit For End If Next c If isBlank Then ws.Rows(r).Delete Next r Application.ScreenUpdating = True MsgBox "Done. Blank rows removed." End Sub - Adjust the column range if needed. If only certain columns matter (for example, a row is blank when column A and column C are both empty), replace the inner loop with explicit checks such as
If Len(Trim(ws.Cells(r, "A").Value)) = 0 And Len(Trim(ws.Cells(r, "C").Value)) = 0 Then. To limit to a fixed span, changelastColto a number like 8. - Select the sheet you want to clean. The macro works on the active sheet, so click the correct tab before running it.
- Run the macro. Press F5 in the editor, or go back to Excel and use Developer > Macros, select DeleteBlankRows, and click Run. A message box confirms completion.
- Check the result and save. Scroll through the sheet, confirm your headers and totals survived, then save as .xlsm. If anything looks wrong, close without saving and reopen your backup.
Common Mistakes
- Looping top to bottom. Deleting row 5 moves the old row 6 to row 5, so the counter jumps past a row. Always use
For r = lastRow To 1 Step -1. - Checking only one column. A row with a name in column A but nothing else is kept by one-column checks and deleted by others. Decide what "blank" means for your data before you run anything.
- Treating spaces and formulas as empty. Cells containing a space, an apostrophe, or a formula returning "" look blank but are not empty. The
Trimcheck above handles stray spaces; formulas returning empty text still count as content, so clean or convert them first if they should be removed. - Using
UsedRange.Rows.Countblindly. The used range often extends far below your data because of old formatting, which makes the loop crawl and can delete formatted spacer rows. - Forgetting to save as .xlsm. The macro disappears the next time the file is opened.
- Running on the wrong sheet. Since the macro uses
ActiveSheet, clicking a summary tab and running it will strip that tab instead of your data. - Assuming Ctrl+Z will save you. VBA deletions are not placed on the undo stack.
Tips
- Turn off
Application.ScreenUpdatingand, for large ranges,Application.Calculationduring the loop (set it back toxlCalculationAutomaticafterwards). This can cut run time dramatically on sheets with thousands of rows. - Add a counter such as
deleted = deleted + 1and report it in the message box so you know exactly how many rows went. - For a quick one-off job on a small table, Filter > Blanks on your key column, then delete the visible rows is faster than writing code. Macros earn their keep when the task repeats.
- To clean several sheets at once, wrap the body in
For Each ws In ActiveWorkbook.Worksheetsand skip any sheet whose name you want protected. - Keep the macro in a personal macro workbook (PERSONAL.XLSB) if you want it available in every file without pasting it each time.
- If your table has a header row, start the loop at
lastRow To 2so the header can never be deleted.
Troubleshooting
The macro deletes nothing. Confirm you selected the right sheet and that the data actually lives in the columns you are checking. Cells that look empty but hold spaces need the Trim comparison shown above. Also check that no filter is hiding rows: hidden rows are still deleted, and filtered-out ones still count as present, which is a common source of confusion.
"Run-time error '91'" or "'424' Object required". These usually mean the Find call returned nothing because the sheet is empty. Guard against it by testing If ws.Cells.Find(...) Is Nothing Then Exit Sub before using .Row.
Merged cells cause rows to vanish oddly. Deleting a row that intersects a merged range can behave unpredictably. Unmerge, clean, then re-merge if needed.
It deletes rows you wanted to keep. Close the file without saving, reopen your backup, and narrow the blank test to specific columns. Rows that count as "blank" purely because values sit in another column are the usual culprit.
It runs very slowly. Limit the range to your real last column and row, disable screen updating, and avoid reading each cell from the worksheet one at a time on huge sheets; reading the range into an array and testing in memory is far faster.
Macros are blocked. Files from the internet open with macros disabled. Use the security bar to enable them, or add the folder to Trusted Locations via File > Options > Trust Center. If your organisation blocks macros, ask IT rather than trying to bypass the policy.
Frequently Asked Questions
Can VBA delete only rows that are completely empty?
Yes. Test every column in your used range, as the macro above does. A row is deleted only when all of those cells are blank after trimming spaces.
Will the macro remove rows with blanks in a single column?
Only if you write the test that way. Point the check at that one column and rows with an empty value there will be deleted even if other columns hold data. That is often useful for cleaning lists keyed on one field, but it is destructive, so back up first.
Why does my macro skip some blank rows?
Almost always a top-to-bottom loop, or cells containing spaces, apostrophes or formulas returning empty text. Loop backwards and trim values.
Can I undo a VBA row deletion?
No. Ctrl+Z does not restore rows deleted by VBA. Reopen a saved copy, or use version history if the file is stored in OneDrive or SharePoint.
Does this work on Excel for Mac?
The code is the same, but the path to the editor differs and menu labels vary by version. Open the Visual Basic Editor from the Tools menu and insert a module there.
Can I delete blank rows without a macro?
Yes. Select your data, use Data > Filter, filter Blanks on a key column, then right-click the visible row numbers and delete them. Macros are better when you repeat the task on new data regularly.