Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Remove Row: Delete Rows with a Macro

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To remove a row in Excel VBA, use the Rows or Range object and call its Delete method. For example, Rows(5).Delete removes row 5, and Range("A10").EntireRow.Delete removes the entire row containing cell A10. To remove rows based on a condition, loop through the range and delete only the rows that meet your test. The one rule that prevents most problems: when looping and deleting, step backwards (For i = lastRow To 1 Step -1) or you will skip rows.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer on Windows; Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook you can test on — save a copy first, because a macro that deletes rows cannot be undone with Ctrl+Z after it runs.
  • Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and inserting a module (Insert > Module).

Menu labels can differ slightly by Excel version, platform and region, but the objects and methods below are stable across current versions.

Step-by-Step

  1. Make a backup copy. Save your workbook as a copy before running any delete macro. VBA deletions are permanent for that session and cannot be reversed with Undo.
  2. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project Explorer, right-click your workbook, choose Insert > Module.
  3. Write a simple single-row delete. In the module, type:
    Sub DeleteRowFive()
        Rows(5).Delete
    End Sub
    Place the cursor inside the sub and press F5 to run it. Row 5 is removed and rows below shift up.
  4. Delete a row by a cell reference. If you know a cell in the row, use Range("A10").EntireRow.Delete. This is safer than a hard-coded row number when the data may move.
  5. Delete several rows at once. Use a range covering whole rows: Rows("5:7").Delete removes rows 5, 6 and 7 together. Deleting in one call is faster than looping when the rows are contiguous.
  6. Delete rows that meet a condition. Loop backwards from the last used row to row 1 and test each row. Example: delete every row where column A equals "Closed".
    Sub DeleteClosedRows()
        Dim lastRow As Long, i As Long
        lastRow = Cells(Rows.Count, "A").End(xlUp).Row
        For i = lastRow To 1 Step -1
            If Trim(Cells(i, "A").Value) = "Closed" Then
                Rows(i).Delete
            End If
        Next i
    End Sub
    The Step -1 is essential: deleting forward makes the loop skip the row that shifts up into the deleted row's place.
  7. Delete blank rows. Test for an empty key column rather than the whole row, so rows with partial data are not lost: If Application.CountA(Rows(i)) = 0 Then Rows(i).Delete removes only fully empty rows. To remove rows where a specific column is blank, test Cells(i, "A").Value = "".
  8. Turn off screen updating for large deletes. For thousands of rows, wrap the loop with Application.ScreenUpdating = False at the start and Application.ScreenUpdating = True at the end. This speeds up the macro noticeably. Do not disable DisplayAlerts unless you understand it suppresses prompts.
  9. Run and verify. Press F5, then check the row count and a few remaining records. If the result is wrong, close without saving and reopen your backup.

Common Mistakes

  • Looping forward while deleting. For i = 1 To lastRow with Rows(i).Delete skips rows because each deletion shifts the next row up into the current index. Always loop backwards or collect rows into a Union and delete once.
  • Deleting the header row. Start loops at row 2, not row 1, when row 1 holds headers.
  • Using ClearContents instead of Delete. ClearContents empties the cells but leaves the row in place, so your row count does not change.
  • Hard-coding the last row. A fixed number like 1000 breaks when data grows. Use Cells(Rows.Count, "A").End(xlUp).Row to find the real last row.
  • Forgetting that deleting rows shifts references. Formulas, named ranges and charts that point at deleted rows may return #REF! errors. Check dependent sheets after running.
  • Running the macro on the wrong sheet. Unqualified Rows(i) acts on the active sheet. Qualify with a worksheet object, e.g. ws.Rows(i).Delete, to avoid deleting from the wrong place.

Tips

  • Use Application.Union to gather all target rows and delete them in a single call — much faster than deleting one at a time on large datasets.
  • Test on a copy with a small dataset first, then scale up.
  • Add a confirmation prompt before destructive deletes: If MsgBox("Delete these rows?", vbYesNo) = vbYes Then ....
  • Store the last row in a Long variable and re-check it if other code inserts rows mid-run.
  • Use EntireRow.Delete rather than selecting the row first — selecting slows the macro and is unnecessary.

Troubleshooting

  • Macro runs but nothing is deleted. Check the condition: values may have trailing spaces (use Trim), be numbers stored as text, or be case-sensitive. Also confirm the macro is acting on the intended sheet.
  • Some matching rows remain. Almost always a forward loop. Change to Step -1.
  • Run-time error 1004. Often caused by deleting rows on a protected sheet or referencing an invalid range. Unprotect the sheet first (Review > Unprotect Sheet) or handle protection in code.
  • Excel freezes on large deletes. Disable screen updating and calculation (Application.Calculation = xlCalculationManual), then restore both at the end.
  • #REF! errors appear elsewhere. Formulas referenced deleted rows. Undo is not available after a macro; restore from your backup and adjust the macro to delete only what is needed.

Frequently Asked Questions

How do I delete a row in VBA without deleting the entire row?

You cannot delete part of a row — Excel rows are all-or-nothing. To remove only some cells, clear them with Range("A5:C5").ClearContents instead.

Why does my loop skip rows when deleting?

Because deleting row i moves the next row up into position i, and the loop then moves to i+1. Loop backwards with Step -1 or build a union of rows and delete once.

Can I undo a row deletion performed by a macro?

No. Ctrl+Z does not undo VBA deletions. Always keep a backup or save before running the macro.

How do I delete all rows below a certain row?

Use Rows("20:" & Rows.Count).Delete to remove everything from row 20 to the bottom of the sheet.

How do I delete rows containing a specific word?

Test with InStr or Like, for example If InStr(1, Cells(i, "A").Value, "test", vbTextCompare) > 0 Then Rows(i).Delete, looping backwards.

Does deleting rows with VBA affect other sheets?

Only if formulas or references on other sheets point at the deleted rows. Check for #REF! errors after running.