Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all data rows from an Excel table (apart from the first)

Tags:

Just recently I've been trying to delete all data rows in a table, apart from the first (which needs to just be cleared)

Some of the tables being actioned could already have no rows, so I was running it to problems as using .DataBodyRange.Rows.Count on a table with no rows (just header and/or footer) causes errors.

I looked all over for a solution an could not find a whole one, so I hope my answer to this question will be useful to others in the future.

like image 398
David Gard Avatar asked Dec 18 '13 16:12

David Gard


People also ask

How do I remove all rows in Excel except header?

Press the F5 key to run the code, then all rows except the first header row are deleted from the active worksheet immediately.

How do I delete a row from a table in Excel VBA?

To delete an entire row in Excel using VBA, you need to use the EntireRow. Delete method. The above code first specifies the row that needs to be deleted (which is done by specifying the number in bracket) and then uses the EntireRow. Delete method to delete it.

How do you delete certain rows in Excel?

Select the cells, rows, or columns that you want to delete. Right-click, and then select the appropriate delete option, for example, Delete Cells & Shift Up, Delete Cells & Shift Left, Delete Rows, or Delete Columns.


1 Answers

This is how I clear the data:

Sub Macro3()     With Sheet1.ListObjects("Table1")         If Not .DataBodyRange Is Nothing Then             .DataBodyRange.Delete         End If     End With End Sub 
like image 168
royUK Avatar answered Oct 08 '22 02:10

royUK