Delete the last 3 rows with VBA

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Paste in Sheet code module of the Sheet you want to affect.
VBA Code:
Sub DeleteLast3()
Dim lRow As Integer, i As Integer
lRow = Me.UsedRange.Rows.Count
For i = lRow To lRow - 2 Step -1
    Me.Rows(i).EntireRow.Delete
Next i
End Sub
 
Upvote 0
1731804724162.png

blockAAttrblockbNR1NR2
12:00:00 AM22980
12:00:00 AM2OMO10
12:00:00 AM2IDR10
12:00:00 AM2ICP21
12:00:00 AM2IMS10
12:00:00 AM2IVL10
12:00:00 AM2ITO20
12:00:00 AM2ITG11
12:00:00 AM2IFG10
12:00:00 AM23790
12:00:00 AM2ISH10
282
484
1
 
Upvote 0
yes the updated code:


Sub DeleteLast3()
Dim lRow As Integer, i As Integer
lRow = ActiveSheet.UsedRange.Rows.Count
For i = lRow To lRow - 2 Step -1
ActiveSheet.Rows(i).EntireRow.Delete
Next i
End Sub


the message
1731954662855.png
 
Upvote 0
For your rows with data, is there always one column that will be populated, like column A?
If so, you can do something like this (no loops needed!)
VBA Code:
Sub MyDelete()

    Dim lr As Long
    
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Delete last three rows
    If lr >= 3 Then Rows(lr - 2 & ":" & lr).Delete

End Sub
 
Upvote 0
Just another option (although I would think post 6 is more efficient), again assumes the last row with data is column A

VBA Code:
Sub DeleterowAgain()
    Dim LstCell As Range

    Set LstCell = Cells(Rows.Count, "A").End(xlUp)

    With LstCell
        If .Row >= 3 Then .Offset(-2).Resize(3).EntireRow.Delete
    End With

End Sub

Btw
yes the updated code:


Sub DeleteLast3()
Dim lRow As Integer, i As Integer

VBA Code:
Dim lRow As Integer, i As Integer

Should be

VBA Code:
Dim lRow As Long, i As Long
 
Upvote 0
Change reference for Sheet1 as required and "Select" to "Delete" when happy that it does what you want.
Code:
Sub Or_So_Maybe()
    Sheets("Sheet1").Cells(Sheets("Sheet1").Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row, 1).Offset(-2).Resize(3).EntireRow.Select
End Sub
 
Upvote 0
Doesn't really need the 2nd
VBA Code:
Sheets("Sheet1").Cells(

VBA Code:
Sub Or_So_Maybe2()
   Sheets("Sheet1").Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Offset(-2).Resize(3).EntireRow.Select
End Sub

does the same as Find is already finding a cell in the row
 
Upvote 0
Hi Mark.
Habit.
There seems to be more problems with not having it where it should be than having it where it does not need to be.
A habit where it does not hurt even if it is not needed.
Thanks for pointing it out though Mark. Appreciated.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
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