code to delete table rows so more rows can then be entered

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,148
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a table with a delete all rows button that is meant to delete all the rows so more rows can be added. In the past, it has deleted all the rows but left one blank row and this allowed me to add more rows but now when I press it, it deletes even the last row, leaving just the header.

How do I make it so there is still one row left, but it is blank when I press the button?

Here is the delete lines 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

Thanks
 

Some videos you may like

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,050
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
If it has worked in the past AND you haven't made any modifications, it should work now !!
Has there been any changes to the layout of the "Costing Tool" Sheet
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,619
Office Version
  1. 2007
Platform
  1. Windows
it deletes even the last row, leaving just the header.

How do I make it so there is still one row left, but it is blank when I press the button?

So it does leave a row, but blank?
The last part of the macro deletes the contents of the cells, try this again:

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
  End With
End Sub
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,148
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
It doesn't leave anything except the header.
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,148
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Thanks for the code Dante, I will try it when I get back to work on Wednesday.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,632
Messages
5,625,994
Members
416,149
Latest member
Bigpotato 668

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
Top