Easy Question for deleting rows.

S

SITCFanTN

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
 
R

Ron de Bruin

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
 
C

Chip Pearson

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]...
 
D

Don Guillett

or

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top