delete bottom rows
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Delete the bottom 3 rows in a table

Delete the bottom 3 rows in a table

resolvedResolved · High Priority · Version 2000

replyReply Wed 2 Feb 2011, 20:46Delegate Davinder said...

Davinder has attended:
Excel VBA Intro Intermediate course

Delete the bottom 3 rows in a table

Hello,

I have a spreadsheet with a number of fields of data in a tabular format. New entries are constantly added to the bottom of this table increasing the total number of rows in the table. I need to create a macro in VBA which automatically identifies the bottom 3 rows in the spreadsheet and then deletes them. I understand that I need to identify the bottom 3 rows of the range of data and then use a delete command to remove these rows but I am not sure of the code that I need to use. Please can you advise how I go about creating the code to do this.

Thank you.

For upcoming training course dates see: Pricing & availability

replyReply Thu 3 Feb 2011, 08:41Trainer Simon said...

RE: Delete the bottom 3 rows in a table

Hi Davinder,

Thank you for your question.

Try this bit of code on some test data first and make sure it is doing exactly what you wanted. It will prompt and ask you how many rows need to be deleted. The default has been set to three relating to your request.

Sub TrimAllSheets()

Dim cs As String
cs = ActiveSheet.Name
Dim y As Integer
y = Application.InputBox("How many bottom rows do you wish to delete?", _
Default:=3, Type:=1) 'Change default number (3) if desired.
If MsgBox("Are you sure you wish to delete " & y & " rows from the bottom of ALL sheets?", _
vbYesNo, "Trim ALL Sheets") = vbNo Then Exit Sub
Application.ScreenUpdating = False
Dim r As Range, s As Range
Dim ws As Worksheet
On Error Resume Next 'Error handler
For Each ws In ThisWorkbook.Worksheets
ws.Activate
Set r = ActiveSheet.Range("A65536").End(xlUp).Offset(-y + 1)
Set s = ActiveSheet.Range("A65536").End(xlUp)
If ActiveCell.Row < 10 Then GoTo circumv 'Not to delete Headers
Range(r, s).EntireRow.Delete
circumv:
Next ws
Sheets(cs).Activate
Application.ScreenUpdating = True

End Sub

I hope this answers your question.

Let me know.

Regards

Simon

replyReply Thu 3 Feb 2011, 19:17 Edited on Thu 3 Feb 2011, 19:20Delegate Davinder said...

RE: Delete the bottom 3 rows in a table

Hi Simon,

Thanks for this, this is great however this solution takes it one step further than I need to go. I don't want it to prompt me to ask me how many rows to delete, I just want it to remove the bottom 3 lines each time as soon as I run the macro. I like the idea that it prompts you to check whether you really want to delete the rows but if you could confirm how the code needs to be amended so it always removes just the bottom 3 lines that would be great!

I also just need it to remove the bottom 3 rows in sheet 1 only and none of the other sheets.

Many thanks,

Dav

replyReply Thu 3 Feb 2011, 08:45Trainer Simon said...

RE: Delete the bottom 3 rows in a table

Hi Davinder,

Just a quick addition to the last email the code deletes the last three rows from every sheet in the workbook so you can adapt the code and remove the references relating to the looping procedure that loops through all the sheets.

Regards

Simon

replyReply Mon 7 Feb 2011, 17:01Trainer Simon said...

RE: Delete the bottom 3 rows in a table

Hi Davinda,

Try this code.

Sorry for the delay but work has been manic.
-----------------------------------------------------------
Sub DeleteLastThreeRows()

Sheets("Sheet1").Select

Cells(Rows.Count, ActiveCell.Column).End(xlUp).Offset(-2).Resize(3).EntireRow.Select

Selection.Delete

End Sub
----------------------------------------------------------

When you run the macro make sure the sheet1 is active and you must be in a cell in the data.

Test it first on some sample data.

Regards

Simon

replyReply Tue 8 Feb 2011, 19:20Delegate Davinder said...

RE: Delete the bottom 3 rows in a table

Hi Simon this is good - but is there anyway I can get this to work without having to select a cell that is apart of the table range. I.e. I want the code to automatically identify where the data is and remove the last 3 lines.

Thanks.

 

Please browse our web site to find out more about
excel computer training and other Microsoft training courses.

Excel tip:

Quickly insert a function

In Excel 97 and 2000 it was known as the Paste Function dialog box, these days it's known as the Insert Function dialog box. Regardless, one has to choose Insert|Function. or the fx button to open it up. There is, however, a non-mousey way to get hold of the Insert Function dialog box: press Shift+F3 in a blank cell to open the Insert Function dialog.

Press Shift+F3 after a function name and open bracket to open the Function Arguments dialog. For example, type =VLOOKUP( into a cell and press Shift+F3 to obtain a detailed description of VLOOKUP's arguments.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard