Delete all data rows from an Excel table (apart from the first)
Categories:
Efficiently Clear Excel Table Data (Excluding Header Row)

Learn how to programmatically delete all data rows from an Excel table using VBA, while preserving the header row. This guide covers methods for Excel 2007 and later.
When working with Excel tables, you often need to clear out old data before importing new information. A common requirement is to delete all rows except for the header row, which defines the table's structure. Manually doing this can be tedious and error-prone, especially with large datasets. This article provides a robust VBA solution to automate this task, ensuring your table headers remain intact.
Understanding Excel Tables and VBA
Excel tables (introduced in Excel 2007 as 'ListObjects') are powerful tools for managing structured data. They offer features like automatic range expansion, banded rows, and structured references. When you delete rows from an Excel table, it's crucial to interact with the ListObject
itself rather than just a range of cells, to maintain the table's integrity and properties.
VBA (Visual Basic for Applications) provides the necessary automation capabilities. We'll leverage the ListObject
object model to identify the table, determine its data range, and then delete the rows efficiently.
flowchart TD A[Start] --> B{"Is there an active table?"} B -->|No| C[Exit: No table found] B -->|Yes| D[Identify ListObject] D --> E[Check if table has data rows] E -->|No data rows| C E -->|Has data rows| F[Select DataBodyRange] F --> G[Delete rows from DataBodyRange] G --> H[End]
Flowchart for deleting data rows from an Excel table.
The VBA Solution: Deleting Data Rows
The core of the solution involves identifying the ListObject
you want to modify and then targeting its DataBodyRange
. The DataBodyRange
property specifically refers to the range of cells that contain the table's data, excluding the header row and any total rows. By deleting this range, we ensure that only the data is removed, leaving the table structure and header intact.
Sub DeleteAllTableRowsExceptHeader()
Dim ws As Worksheet
Dim lo As ListObject
Dim tblName As String
' Set the worksheet where your table is located
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name
' *** IMPORTANT: Change "Table1" to the actual name of your table ***
tblName = "Table1"
On Error GoTo ErrorHandler
' Check if the table exists on the specified worksheet
Set lo = ws.ListObjects(tblName)
If Not lo Is Nothing Then
' Check if the table has any data rows
If Not lo.DataBodyRange Is Nothing Then
' Delete all rows in the DataBodyRange
lo.DataBodyRange.Delete
MsgBox "All data rows from table '" & tblName & "' have been deleted.", vbInformation
Else
MsgBox "Table '" & tblName & "' has no data rows to delete.", vbInformation
End If
Else
MsgBox "Table '" & tblName & "' not found on sheet '" & ws.Name & "'.", vbExclamation
End If
Exit Sub
ErrorHandler:
MsgBox "An error occurred: " & Err.Description, vbCritical
End Sub
VBA code to delete all data rows from a specified Excel table, preserving the header.
How to Use the VBA Code
To implement this solution, you'll need to open the VBA editor in Excel, insert a new module, and paste the provided code. Remember to customize the sheet name and table name to match your specific workbook.
1. Open the VBA Editor
Press Alt + F11
in Excel to open the Visual Basic for Applications editor.
2. Insert a New Module
In the VBA editor, right-click on your workbook name in the 'Project Explorer' pane (usually on the left), then select Insert > Module
.
3. Paste the Code
Copy the VBA code provided above and paste it into the newly created module window.
4. Customize Sheet and Table Names
Modify the lines Set ws = ThisWorkbook.Sheets("Sheet1")
and tblName = "Table1"
to reflect the actual name of your worksheet and Excel table.
5. Run the Macro
You can run the macro by placing your cursor anywhere within the Sub DeleteAllTableRowsExceptHeader()
procedure and pressing F5
, or by going to Developer > Macros
in Excel, selecting DeleteAllTableRowsExceptHeader
, and clicking Run
.
DataBodyRange.Delete
method will correctly ignore it, as the Total Row is not part of the DataBodyRange
. However, if you have other custom rows or merged cells within your table's data area, they will be deleted.