ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Select last n rows and delete? (https://www.excelbanter.com/excel-programming/352127-select-last-n-rows-delete.html)

JD

Select last n rows and delete?
 
I've done a search and nothing quite seems to fit. Basically I merely
want to select the last /n/ rows in a certain column and then delete
those entire rows.
n being four for the forseeable future.

It started looking like this:
//---
Range("B" & Rows.Count).End(xlUp).Select
Selection.Offset(RowOffset:=-3).Select
//---
But anything after doesn't do what I'd want.

I appreciate a suggestion.


Norman Jones

Select last n rows and delete?
 
Hi JD,

Try:

'=============
Public Sub TesterD()

Dim n As Long

n = 4

Cells(Rows.Count, "B").End(xlUp).Offset(n - 1).Resize(n).Delete

End Sub
'<<=============


---
Regards,
Norman



"JD" wrote in message
ups.com...
I've done a search and nothing quite seems to fit. Basically I merely
want to select the last /n/ rows in a certain column and then delete
those entire rows.
n being four for the forseeable future.

It started looking like this:
//---
Range("B" & Rows.Count).End(xlUp).Select
Selection.Offset(RowOffset:=-3).Select
//---
But anything after doesn't do what I'd want.

I appreciate a suggestion.




Norman Jones

Select last n rows and delete?
 
Hi JD,

Correcting, try:

'=============
Public Sub TesterD()

Dim n As Long

n = 4

Cells(Rows.Count, "B").End(xlUp).Offset(1 - n). _
Resize(n).EntireRow.Delete
End Sub
'<<=============


--
Regards,
Norman



JD

Select last n rows and delete?
 
Norman Jones wrote:
Hi JD,

Correcting, try:

'=============
Public Sub TesterD()

Dim n As Long

n = 4

Cells(Rows.Count, "B").End(xlUp).Offset(1 - n). _
Resize(n).EntireRow.Delete
End Sub
'<<=============


That one works perfectly, thanks. Question; the resize works as a range
because the variable n has been declared?


Norman Jones

Select last n rows and delete?
 
Hi JD,

That one works perfectly, thanks. Question; the resize works as a range
because the variable n has been declared?


The Resize Property returns a resized range.

The syntax for Resize is RangeExpression(RowSize, ColumnSize).

The suggested code uses an abbreviated form, Resize(n), which leaves the
ColumnSize of the range unchanged.


---
Regards,
Norman



"JD" wrote in message
oups.com...
Norman Jones wrote:
Hi JD,

Correcting, try:

'=============
Public Sub TesterD()

Dim n As Long

n = 4

Cells(Rows.Count, "B").End(xlUp).Offset(1 - n). _

Resize(n).EntireRow.Delete
End Sub
'<<=============


That one works perfectly, thanks. Question; the resize works as a range
because the variable n has been declared?





All times are GMT +1. The time now is 02:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com