PDA

View Full Version : VBA To Extract Latest X Number Of Rows From A Range and Paste To Another



mrprofit
04-10-2014, 11:57 PM
I have a range of data from A1:F5000, what is the code to extract the last/newest 500 rows of the data and paste into H:M

jolivanes
04-11-2014, 10:14 AM
You could try this

Range("A" & Rows.Count).End(xlUp)(-498).Resize(500,6).Copy Range(H1")

mrprofit
04-11-2014, 12:42 PM
the code shows an error when the no. of rows in the range is lesser than 500 rows, how to correct it, thank you

snb
04-11-2014, 02:30 PM
Why don't you try to come up with a solution yourself ?
The basic ingredients have been provided to you.
If you read the code and analyse it it's not complicated.

mrprofit
04-11-2014, 02:51 PM
problem solved, thank you

Excel Fox
04-11-2014, 03:59 PM
mrprofit, for posterity, it would be great if you could share your solution. Here's my solution



With Range("A" & Rows.Count).End(xlUp)
.Cells(Application.Max(-498, 2 - .Row)).Resize(Application.Min(500, .Row), 6).Copy Range("H1")
End With

mrprofit
04-11-2014, 05:50 PM
My solution, :)


If Sheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row >= 500 Then
Range("A" & Rows.Count).End(xlUp)(-498).Resize(500, 6).Select
Else
Range("A1:F500").Select
.....
End IF

snb
04-11-2014, 06:24 PM
Sub M_snb()
Range("A1:F500").Offset(Abs((Columns(1).SpecialCells(2).Count > 500)) * Columns(1).SpecialCells(2).Count).copy cells(1,8)
End Sub

Rick Rothstein
04-13-2014, 02:38 PM
This single code line should also work...


Cells(Application.Max(1, Cells(Rows.Count, "A").End(xlUp).Row - 499), "A").Resize(500, 6).Copy Range("H1")