Delete method of Range Class failed

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. 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
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
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Your code works for me, and also works without .Rows
.Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Delete
 
Upvote 0
Solution
Thanks. I just tried and mine works fine now too.

Not sure why??
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top