delete lat 7 from table help

seabth

New Member
Joined
Oct 19, 2013
Messages
3
I need to delete the last 7 rows from a table, but never the first 7. here is what I have to delete the last 7, just not sure about the "IF" first 7 part.
VBA Code:
Sub DeleteLast7Rows()

    Dim fname As String
    Dim lr As Long
        

'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Delete last 7 rows of data
    Rows(lr - 6 & ":" & lr).Delete

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Welcome to the Board!

What Excel row is the first row of data on?

If you have something like 10 rows of data to start, then you cannot delete 7, as then you would be left with 3 (which is less than your 7 minimum).
In that case, do you just want to delete 3 to get down to 7, or not delete any at all if you cannot delete 7?
 
Upvote 0
Welcome to the Board!

What Excel row is the first row of data on?

If you have something like 10 rows of data to start, then you cannot delete 7, as then you would be left with 3 (which is less than your 7 minimum).
In that case, do you just want to delete 3 to get down to 7, or not delete any at all if you cannot delete 7?
The first 7 rows will always have data... data will be added 7 rows at a time starting at row 8.
 
Upvote 0
The first 7 rows will always have data... data will be added 7 rows at a time starting at row 8.
So then I think this is all you need:
Rich (BB code):
Sub DeleteLast7Rows()

    Dim fname As String
    Dim lr As Long
       

'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
   
'   Delete last 7 rows of data
    If lr > 7 Then Rows(lr - 6 & ":" & lr).Delete

End Sub
 
Upvote 0
Solution
So then I think this is all you need:
Rich (BB code):
Sub DeleteLast7Rows()

    Dim fname As String
    Dim lr As Long
      

'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
  
'   Delete last 7 rows of data
    If lr > 7 Then Rows(lr - 6 & ":" & lr).Delete

End Sub
thanks, that works.
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,850
Members
449,051
Latest member
excelquestion515

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