To clear the data rows in an Excel table with VBA while keeping the table, headers, formatting, and structure, use DataBodyRange.ClearContents. For example, If Not lo.DataBodyRange Is Nothing Then lo.DataBodyRange.ClearContents clears the values and formulas in the table’s body; it does not remove the table itself.
What “clear table contents” means
An Excel table, also called a ListObject in VBA, has a header row, a body of data rows, and optionally a totals row. “Clear contents” can mean several different things, so choose the operation that matches what you want to preserve:
- Clear body contents: removes constants and formulas from the data cells, leaving the table and its rows in place.
- Clear everything from body cells: removes contents and cell formatting, but still leaves the table structure and table style.
- Delete table rows: removes the records and shrinks the table, while keeping the table and its headers.
- Convert the table to a normal range or delete the table: changes or removes the table structure, not just its contents.
In most cases, when someone asks how to clear an Excel table using VBA, the first option is intended. Be aware that clearing contents leaves blank data rows inside the table. That differs from deleting rows, which reduces the table’s size.
Clear the data rows and keep the table
The following macro finds a table named SalesTable on a worksheet named Sales, then clears its data cells. Change both names to match your workbook. The check for Nothing handles a table with no body rows, such as a newly created table containing only headers.
Sub ClearSalesTableContents()
Dim ws As Worksheet
Dim lo As ListObject
Set ws = ThisWorkbook.Worksheets("Sales")
Set lo = ws.ListObjects("SalesTable")
' Clear data cells only; retain the table and headers.
If Not lo.DataBodyRange Is Nothing Then
lo.DataBodyRange.ClearContents
End If
End Sub
ClearContents clears both typed values and formulas. It does not clear formatting, and it does not clear the header row or a totals row because those are outside DataBodyRange. The table’s banded rows, column names, filters, and structured-reference identity remain available. If formulas are present in the body, they will be removed too, so preserve or restore them if they are meant to remain.
Before running any macro, save a copy of the workbook. VBA actions cannot be undone with Ctrl+Z. To use this code in Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, paste the code, and run the procedure. On Mac, open the Visual Basic Editor from the Developer tab or Tools > Macro > Visual Basic Editor, depending on the ribbon setup; VBA is not available in Excel for the web.
Clear values in selected table columns
To clear only specific columns, address those columns through the table’s ListColumns collection. This example clears the body cells in the Status column without affecting the other fields.
Sub ClearStatusColumn()
Dim ws As Worksheet
Dim lo As ListObject
Set ws = ThisWorkbook.Worksheets("Sales")
Set lo = ws.ListObjects("SalesTable")
' Clear only the Status column's data cells.
If Not lo.DataBodyRange Is Nothing Then
lo.ListColumns("Status").DataBodyRange.ClearContents
End If
End Sub
Use the exact header text in ListColumns; a renamed or misspelled heading causes a runtime error. If the table has no data rows, the outer check prevents trying to access a body range that does not exist. For a more defensive macro that can be run across workbooks where the column may be absent, check the table’s column names before accessing it.
Clear formatting, delete rows, or remove the table
Use a different method when “clear” means more than removing values. Clear clears cell contents and direct cell formatting from the selected range. A table style may still display formatting because it belongs to the table, not just to the cells.
Sub ClearTableBodyContentsAndCellFormatting()
Dim ws As Worksheet
Dim lo As ListObject
Set ws = ThisWorkbook.Worksheets("Sales")
Set lo = ws.ListObjects("SalesTable")
' Clear body contents and direct cell formatting.
If Not lo.DataBodyRange Is Nothing Then
lo.DataBodyRange.Clear
End If
End Sub
To remove all data rows while retaining the headers and table, delete the table’s DataBodyRange rows instead. This changes the table’s size, so confirm that is what you intend before running it.
Sub DeleteSalesTableRows()
Dim ws As Worksheet
Dim lo As ListObject
Set ws = ThisWorkbook.Worksheets("Sales")
Set lo = ws.ListObjects("SalesTable")
' Remove all data rows but retain the table and its headers.
If Not lo.DataBodyRange Is Nothing Then
lo.DataBodyRange.Delete
End If
End Sub
To remove the table object but leave its cells as a regular range, use lo.Unlist. To delete the worksheet cells themselves, use a worksheet range’s Delete method, but take care: deleting cells can shift neighboring data. These are structural changes, not equivalent to clearing contents.
Choose a table by worksheet or loop through tables
When a worksheet has several tables, refer to each by its table name rather than guessing its cell address. Table names are unique within a workbook, and the worksheet’s ListObjects collection makes the relationship explicit. To clear every table on a sheet, loop through that sheet’s tables:
Sub ClearAllTablesOnSheet()
Dim ws As Worksheet
Dim lo As ListObject
Set ws = ThisWorkbook.Worksheets("Sales")
' Clear each table's body, leaving its headers and structure.
For Each lo In ws.ListObjects
If Not lo.DataBodyRange Is Nothing Then
lo.DataBodyRange.ClearContents
End If
Next lo
End Sub
This affects every table body on the named worksheet, so it is riskier than targeting one table. If the goal is to clear all tables in the workbook, loop over each worksheet and then over its ListObjects; review the workbook first because lookup tables, configuration tables, or formula-driven tables may also be affected.
Common problems and safeguards
- “Subscript out of range”: check that the worksheet name and table name are exact. A table’s displayed name is available under Table Design > Table Name when the table is selected.
- No data rows: a header-only table has no
DataBodyRange. Test whether it isNothingbefore clearing, deleting, or addressing a column’s body. - Unexpected formulas disappear:
ClearContentsclears formulas as well as values. Use a narrower range or column when some calculated columns must stay intact. - Blank rows remain: this is expected. Clearing contents does not delete table rows, so filters and table size still include those rows.
- Macro blocked or unavailable: Excel for the web cannot run VBA. In desktop Excel, use a macro-enabled workbook format such as
.xlsmif you need to retain the macro, and follow your organization’s macro security policy.
For a recurring process, consider adding a confirmation prompt before clearing important data, or create a backup sheet or file first. A macro can make a destructive operation quick, but it cannot restore the cleared data through Excel’s normal Undo command.
Frequently Asked Questions
Does ClearContents remove the Excel table?
No. It clears values and formulas in the specified cells. The table, headers, formatting, and table name remain. To remove the table structure, use Unlist instead.
How do I clear a table but keep its headers?
Clear lo.DataBodyRange, not the entire worksheet range occupied by the table. Check that DataBodyRange is not Nothing first, because a table can have headers but no data rows.
How do I clear one column in an Excel table with VBA?
Use lo.ListColumns("ColumnName").DataBodyRange.ClearContents, with the actual table and header names. Add a check for an empty table before accessing its data range.
What is the difference between ClearContents and deleting table rows?ClearContents leaves the rows in the table but empties their cells. Deleting DataBodyRange removes the records and shrinks the table while preserving its header row.
Can I undo a VBA table-clear macro?
Usually not with Ctrl+Z. Excel clears its normal undo history when a macro runs, so save a separate copy or backup before executing a macro that removes data.
Related
- How to Insert a Table of Contents in Microsoft Word
- How to Insert a Table of Contents in Microsoft Word
- How to Insert a Table of Contents in Word for the Web
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Unique Values: Code, Methods, and Practical TipsTo return unique values with Excel VBA, read the source cells, keep each value only the first time…
- VBA UserForm ScrollBar Property: How to Use ItThe ScrollBar property in VBA UserForms is a control you add from the Toolbox that lets users select…
- Excel UserForm Date Picker: Options and SetupYes, you can add a date picker to an Excel UserForm, but Excel for Microsoft 365 on Windows…
- VBA AutoFilter Operator: Values, Uses, and ExamplesIn VBA, the Operator argument of Range.AutoFilter tells Excel how to combine two criteria for one field or…
- Excel VBA Copy Paste Transpose: Working Code and TipsTo copy a range and paste it transposed with Excel VBA, copy the source range and use PasteSpecial…
- VBA Loop Continue: How to Skip to the Next IterationVBA has no built-in Continue For, Continue Do, or general Continue statement. To skip the rest of the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.