To delete all rows in an Excel table except the header row using VBA, you can use the ListObject's DataBodyRange property to identify and clear rows below the header. This preserves the table structure and header while removing all data rows—ideal for resetting tables before fresh data import or routine cleanup. No external tools or libraries are required—just Excel with VBA enabled.
What You'll Need
- Microsoft Excel (2007 or later, with ListObject/table support)
- A worksheet containing a properly formatted table (created via Insert > Table)
- Developer tab enabled in Excel (to access the VBA editor)
- Basic familiarity with Excel tables and VBA macro security settings
Step-by-Step
- Open your Excel workbook and ensure your data is formatted as a table. Select any cell in the dataset, then press Ctrl+T (Cmd+T on Mac) and confirm the range includes headers.
- Press Alt+F11 (Option+F11 on Mac) to open the Visual Basic for Applications (VBA) editor. Alternatively, go to the Developer tab > Code group > Visual Basic.
- In the VBA editor, locate the Project Explorer window on the left (if hidden, press Ctrl+R to show it). Expand the VBAProject entry for your workbook, then double-click the worksheet containing your table (e.g., Sheet1 (Sheet1)).
- At the top of the code window, paste the following VBA procedure. Replace "TableName" with the actual name of your table (found in Table Design > Table Name):
Sub DeleteAllRowsExceptHeader()
Dim ws As Worksheet
Dim tbl As ListObject
' Set the worksheet (adjust sheet name if needed)
Set ws = ThisWorkbook.Worksheets("Sheet1")
' Set the table object (replace "TableName" with your table's name)
On Error Resume Next
Set tbl = ws.ListObjects("TableName")
On Error GoTo 0
' Exit if table not found
If tbl Is Nothing Then
MsgBox "Table 'TableName' not found on Sheet1. Check the table name and sheet name.", vbExclamation
Exit Sub
End If
' Delete all rows in the data body range, if any exist
If Not tbl.DataBodyRange Is Nothing Then
tbl.DataBodyRange.Delete
End If
MsgBox "All data rows deleted. Header row preserved.", vbInformation
End Sub
- Click anywhere inside the DeleteAllRowsExceptHeader subroutine, then press F5 to run it—or close the editor and run the macro from Excel via Developer tab > Code group > Macros > DeleteAllRowsExceptHeader > Run.
- Verify the result: return to your worksheet and confirm only the header row remains in the table. The table structure, including filters and formatting, should still be intact.
Common Mistakes
- Using the wrong table name—ensure the name in the code matches exactly (case-insensitive, but spelling and spaces must match). Check Table Design > Properties > Table Name in Excel.
- Referencing the wrong worksheet—ensure the worksheet name in the code (e.g., "Sheet1") matches the actual tab name, including any spaces or special characters.
- Running the macro on a range that is not a table—ListObject methods fail if applied to a normal range. Confirm your data is formatted as a table (Ctrl+T).
- Forgetting to enable macros—save the workbook as .xlsm and allow macros when opening. If macros are disabled, the code will not run.
- Deleting the header accidentally—using .Rows.Delete without specifying DataBodyRange removes the header row too. Always target DataBodyRange specifically.
Tips
- For dynamic tables where the name is unknown, use ActiveCell.ListObject to reference the table containing the active cell—e.g., Set tbl = ActiveCell.ListObject.
- To avoid error messages, wrap the core logic in an error handler (On Error GoTo ErrHandler) and clean up resources (e.g., Set tbl = Nothing) before exiting.
- Consider adding a confirmation dialog before deletion for safety: use MsgBox with vbYesNo to prompt the user before proceeding.
- Test the macro on a copy of your data first—especially if the table contains formulas or linked data.
- Combine this with data import or refresh routines to automate report setup—e.g., delete old rows, then paste new data or run a query.
Troubleshooting
If the macro does not delete rows or throws an error, follow these checks:
- Run-time error '9': Subscript out of range—this means the worksheet name is incorrect. Double-check the tab name in Excel and update the code. For example, "Sheet 1" (with a space) differs from "Sheet1".
- Run-time error '1004': Application-defined or object-defined error—this often occurs if the table name is wrong or the table has no data rows. Verify the table name via Table Design > Properties. If the table is empty (only headers), DataBodyRange is Nothing, and the Delete line is skipped—this is safe behavior.
- Macro runs but no rows are deleted—ensure the table actually contains data rows. Also, check if the table is protected: right-click the sheet tab > Unprotect (password required if set). If the sheet is protected, delete protection first or unprotect via VBA (e.g., ws.Unprotect "password").
- Macro does not appear in the Macros list—confirm the module is in the correct workbook (not in Personal Macro Workbook unless intended). Save the file as .xlsm, restart Excel, and re-enable macros.
- Header row disappears—this indicates you used .Rows.Delete instead of .DataBodyRange.Delete. Always use DataBodyRange to exclude the header row. If the header is missing, undo (Ctrl+Z) and rerun the corrected macro.
If issues persist, temporarily simplify the code to isolate the problem: test with a single worksheet and hardcoded table reference, and add Debug.Print statements (e.g., Debug.Print tbl.Name) to verify object assignment.