dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,352
- Office Version
- 365
- 2016
- Platform
- Windows
I have a button to delete all the rows in a table. It used to work and I am not sure what has happened.
This is my code
The line that the code halts on with the 'Delete method of range class failed' error is.
This is my code
VBA Code:
Sub CostingDeleteAll()
'Deleting The Data In A Table
Dim tbl As ListObject
Dim cell As Range
Set tbl = ThisWorkbook.Worksheets("Costing_tool").ListObjects("tblCosting")
'Delete all table rows except first row
With tbl.DataBodyRange
If .Rows.Count > 1 Then
.Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
End If
'Clear the contents, but not delete the formulas
For Each cell In tbl.ListRows(1).Range.Cells
If Not cell.HasFormula Then
cell.Value = ""
End If
Next
End With
End Sub
The line that the code halts on with the 'Delete method of range class failed' error is.
VBA Code:
.Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete