Easy Question for deleting rows.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a report that varies in length each day. I have information at the
end of the report I want to program to delete in an existing macro. What
code would I use to delete the last 5 lines at the end of the report. The
code would go in the active sheet in an existing macro. Thanks so much
 
One way to do this
This example find the last row with data on the sheet with the function

Sub test()
Range("A" & LastRow(ActiveSheet) - 4).Resize(5).EntireRow.Delete
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
 
Try

Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range(Cells(LastRow - 4, "A"), Cells(LastRow,
"A")).EntireRow.Delete


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



message
news:[email protected]...
 
or

Sub deletelast5rows()
lr = Cells(Rows.Count, "a").End(xlUp).Row
Rows(lr - 4).Resize(5).Delete
End Sub
 
Back
Top