I am new to using VBA and would like to add coding to a button that will identify the size of a table called "QA_Activity" and clear all but the header row on clicking the button. I was hoping that this would be quite simple but am struggling as to how to identify a table that could be a different size each month. Many thanks in advance.
-
https://stackoverflow.com/questions/25037467/find-the-first-empty-cell-in-the-same-column-row will help you. – Dylan Brams Apr 11 '18 at 07:35
-
Welcome to Stack Overflow. If you ask questions here please always include what you already have tried. Therefore [edit] your question and add the code you already have. Even if it is not working people here will kindly help you to fix/improve it. But it is very unlikely that they do all the work for you if you if you did nothing or didn't try at all. – Pᴇʜ Apr 11 '18 at 07:45
-
Possible duplicate of [Delete all data rows from an Excel table (apart from the first)](https://stackoverflow.com/questions/20663491/delete-all-data-rows-from-an-excel-table-apart-from-the-first) – Pᴇʜ Apr 11 '18 at 07:45
2 Answers
Tables are called ListObjects in VBA. A ListObject has a property called a .DataBodyRange that contains everything under the header. That has a .Clear method.
I generally use this syntax to clear the body of a ListObject:
Range("Table1").ListObject.DataBodyRange.Clear
The Range("Table1")
bit allows me to find the ListObject even if I don't know what sheet it's on. This works because Tables are also Named Ranges that you can address by name. So you just say "Go to the Named Range called Table1, and get me the ListObject that lives there". Otherwise you would need to know in advance what sheet the ListObject is on, and use the following:
Worksheets("SomeSheet").Listobjects("Table1").DataBodyRange.Clear
...which works just fine until one day you move the Table to a different sheet, and forget to update your code.
Note that a table is not guaranteed to actually have a .DataBodyRange, because someone may have deleted all the rows under the header. For instance, take this Table:
How many rows does it have in the DataBodyRange?
? Range("Table1").ListObject.DataBodyRange.Rows.Count
3
Okay, now I'm going to delete those rows:
...leaving this:
How many rows in that DataBodyRange now?
? Range("Table1").ListObject.DataBodyRange.Rows.Count
Whoops...you can't reference a .DataBodyRange if it don't exist.
So to be safe, stick an On Error Resume Next
before you try to reference a .DataBodyRange, and an On Error Goto 0
afterwards. Or something fancier.

- 4,668
- 1
- 16
- 27
First, create a named range. If required, you can make this dynamic. In this example the named range is Name "Data".
Then Trigger the Sub "Test" from the following VBA code.
Option Explicit
Sub ClearRange(ByVal rngCell As Range)
Dim rngRange As Range
Set rngRange = rngCell.CurrentRegion
rngRange.Range(rngRange.Parent.Cells(2, 1), rngRange.Parent.Cells(rngRange.Rows.Count, rngRange.Columns.Count)).ClearContents
End Sub
Sub test()
Dim rngCell As Range
Set rngCell = ActiveSheet.Range("Data").Cells(1, 1)
ClearRange rngCell
End Sub
This should clear the range except for the first row (headers).
Important: The header row and the first column of your range must be filled completely (no empty cells) for the above to work smoothly.

- 937
- 7
- 14
-
I didn't place it, but looking at your code I'd suggest it's not as simple or robust as it could be. The use of .CurrentRegion could be problematic if other data the OP doesn't want cleared happens to be immediately ajoining the table, and Tables aka ListObjects have built in properties and methods that are much cleaner. – jeffreyweir Apr 11 '18 at 09:53
-
Additionally to the concerns jeffreyweir mentioned: Since table/ListObject names are actually named ranges listed in the NameManager that automatically resizes with the table it seems to be very redundant defining another name for data. You could just use the name of the table which is much more safe and no redundancy. See [The VBA Guide To ListObject Excel Tables](https://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables) for a nice guide on that topic. Only the fact that you could do it like you did doesn't mean that this automatically a good answer/idea. – Pᴇʜ Apr 12 '18 at 06:21