VBA & Macro Problems

Fix Excel VBA EntireRow.Delete Error 1004 After Filtering

Aamir Naveed AkramAamir Naveed Akram Oct 29, 2026 868 views

Question details

The user is experiencing Run-time error 1004 when attempting to delete filtered rows in an Excel table using the VBA EntireRow.Delete method.

How to Fix Excel VBA EntireRow.Delete Failing After Filtering a Table
Product
Excel
Device & OS
not provided
Scenario
Running a VBA macro to filter a table and delete the matching rows.
Observed behavior
The script fails with Run-time error 1004 at the EntireRow.Delete line, which is often caused by hidden columns or hidden rows intersecting the deleted range.
Before you start

Open your VBA editor to review your code, and ensure you have saved a backup copy of your workbook since macro deletions cannot be undone using the standard Undo feature.

Solution 1Recommended

Use SpecialCells to Target Visible Rows Only

Modify your VBA code to explicitly select only the visible cells after filtering, preventing errors caused by hidden data.

When you filter a table, the rows that do not match the criteria are hidden. If you attempt to delete the entire DataBodyRange, VBA may throw Run-time error 1004 because it tries to interact with hidden cells.

Using the SpecialCells(xlCellTypeVisible) method ensures that only the rows currently displayed by the filter are selected and deleted.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor in Excel.

2
Locate your macro module

In the Project Explorer pane on the left, double-click the module that contains your filtering and deletion macro.

3
Update the deletion syntax

Modify your deletion line to include .SpecialCells(xlCellTypeVisible). For example, change it to: .ListObjects("Table1").DataBodyRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete.

4
Test the macro

Save your code and click the 'Run' button (or press F5) to verify that the filtered rows are now deleted without throwing an error.

Use SpecialCells to Target Visible Rows Only
Working Code Example: A working VBA structure looks like this: With Sheet1.ListObjects("Table1").DataBodyRange .AutoFilter Field:=2, Criteria1:="B" .SpecialCells(xlCellTypeVisible).EntireRow.Delete End With
Advanced Spreadsheet Management

Run and Edit VBA Macros Seamlessly in WPS Office

WPS Spreadsheets provides robust support for VBA macros, allowing you to automate tasks, filter tables, and manage data just like in Microsoft Excel.

  1. 1. Install WPS Office: Download and install WPS Office on your computer.
  2. 2. Open your macro workbook: Open your macro-enabled workbook (.xlsm) in WPS Spreadsheets.
  3. 3. Access the Developer tools: Go to the 'Developer' tab on the top ribbon and click 'VBA Editor' to view your scripts.
  4. 4. Edit and run your code: Paste or edit your VBA code to include the SpecialCells method, then click 'Run' to execute the macro smoothly.
Fully compatible with Microsoft Excel (.xlsm) macro-enabled formatsBuilt-in VBA editor for writing and debugging automation scriptsLightweight and fast performance for heavy data processingFree to download with a familiar, easy-to-use ribbon interface
microsoft office alternative - wps office

Frequently Asked Questions

Why does EntireRow.Delete throw Run-time error 1004?

This error typically occurs when the range you are trying to delete intersects with hidden columns or rows, when the worksheet is protected, or when you attempt to delete a non-contiguous range without specifying visible cells.

What does xlCellTypeVisible do in VBA?

The xlCellTypeVisible constant is used with the SpecialCells method to target only the cells that are currently visible on the screen, intentionally ignoring any rows or columns hidden by filters or manual formatting.

Can I undo a macro deletion in Excel?

No, actions performed by a VBA macro, such as deleting rows, clear the application's undo history. You should always save a backup of your workbook before running destructive macros.

How do I unhide all columns using VBA?

You can quickly unhide all columns in the active worksheet by adding the line `ActiveSheet.Columns.EntireColumn.Hidden = False` to your VBA script before any data manipulation occurs.