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.
Press the F5 key to run the code, then all rows except the first header row are deleted from the active worksheet immediately.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With