Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select clear table contents without destroying the table?

I have a vba function in excel 2010 that I built using help from people on here. This function copies the contents of a table/form, sorts them, and sends them to the appropriate tables.

Now after running this function I want the original table to be cleared. I can achieve this with the following code, assuming ACell has been defined as the first cell in the table. ACell.ListObject.Range.ClearContents works fine, the only problem is it deletes the table as well as the data values.

Is there any way around this? I would rather not have to set the table up every time I enter some data.

like image 367
SpeedCrazy Avatar asked Apr 19 '12 02:04

SpeedCrazy


People also ask

How do you clear the contents of a table in Excel?

Select all the cells in the table, click Clear and pick Clear All. Tip: You can also select the table and press Delete. If you want to keep the data without the table format, you won't be able to do that in Excel for the web.

How do I delete a table in Excel without deleting the contents?

To remove a table but keep data and formatting, go to the Design tab Tools group, and click Convert to Range. Or, right-click anywhere within the table, and select Table > Convert to Range.

What is the difference between delete and clear contents in Excel?

clear removes cell contents and certain types of formatting when specified. . delete removes the entire cell and whatever formatting and shifts the rest of the documents in to cover the gap.

How do I clear the contents of a table in Powerpoint?

If a single cell is highlighted the backspace or delete key will clear the cell but not delete it. If an entire row, column or table is selected then the backspace or delete key will delete that row, column or table.


2 Answers

How about:

ACell.ListObject.DataBodyRange.Rows.Delete

That will keep your table structure and headings, but clear all the data and rows.

EDIT: I'm going to just modify a section of my answer from your previous post, as it does mostly what you want. This leaves just one row:

With loSource
   .Range.AutoFilter
   .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
   .DataBodyRange.Rows(1).Specialcells(xlCellTypeConstants).ClearContents
End With

If you want to leave all the rows intact with their formulas and whatnot, just do:

With loSource
   .Range.AutoFilter
   .DataBodyRange.Specialcells(xlCellTypeConstants).ClearContents
End With

Which is close to what @Readify suggested, except it won't clear formulas.

like image 53
Doug Glancy Avatar answered Sep 18 '22 06:09

Doug Glancy


Try just clearing the data (not the entire table including headers):

ACell.ListObject.DataBodyRange.ClearContents
like image 43
Reafidy Avatar answered Sep 18 '22 06:09

Reafidy