vba to delete last row from a table

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
What is the vba code I can add to a button to delete last row from a table?
 
Re: Deleting cells from the bottom of a table

you seem to have two current threads on the front page of the forum right now about this very question

I also don't know why???
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Re: Deleting cells from the bottom of a table

Or try this with a If Statement:

Code:
Private Sub CommandButton2_Click()
'Modified  8/30/2018  7:36:35 PM  EDT
   Dim ans As Long
    With ActiveSheet.ListObjects("npss_quote").DataBodyRange
        ans = .Rows.Count
        If ans > 1 Then .Rows(ans).Delete
    
    End With
End Sub
 
Upvote 0
Re: Deleting cells from the bottom of a table

I made a mistake then couldn't delete it
 
Upvote 0
Re: Deleting cells from the bottom of a table

Problem is that it will delete all the rows in your table if you let it, including all your formulas etc. How do I make it so all the rows can be deleted except the first row, but if you press delete when there is only one row left, it will just clear the row of data and not formulas?
 
Upvote 0
Re: Deleting cells from the bottom of a table

If you use my second script it will not let you delete first row in Table.
 
Upvote 0
Re: Deleting cells from the bottom of a table

If you want to clear contents of Row(1) after all other rows are deleted
Try this:
Code:
Private Sub CommandButton2_Click()
'Modified  8/30/2018  8:21:30 PM  EDT
   Dim ans As Long
    With ActiveSheet.ListObjects("npss_quote").DataBodyRange
        ans = .Rows.Count
        If ans > 1 Then .Rows(ans).Delete
        If ans = 1 Then .Rows(1).ClearContents
    End With
End Sub
 
Upvote 0
Re: Deleting cells from the bottom of a table

If you want to clear contents of Row(1) after all other rows are deleted
Try this:
Code:
Private Sub CommandButton2_Click()
'Modified  8/30/2018  8:21:30 PM  EDT
   Dim ans As Long
    With ActiveSheet.ListObjects("npss_quote").DataBodyRange
        ans = .Rows.Count
        If ans > 1 Then .Rows(ans).Delete
        If ans = 1 Then .Rows(1).ClearContents
    End With
End Sub

That code deleted the top row formulas.
 
Upvote 0
Re: Deleting cells from the bottom of a table

My mistake.
Try this:
Code:
Private Sub CommandButton2_Click()
'Modified  8/30/2018  9:24:30 PM  EDT
   Dim ans As Long
    With ActiveSheet.ListObjects("npss_quote").DataBodyRange
        ans = .Rows.Count
        If ans > 1 Then .Rows(ans).Delete
        If ans = 1 Then .Rows(1).Cells.SpecialCells(xlCellTypeConstants).ClearContents
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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