Selecting, copying and pasting the last 10 rows of a data set

mavara

New Member
Joined
Dec 17, 2016
Messages
9
Hi guys,

I am a recent VBA practionier so I guess the following question will be easy for most of you.
I have a large data set. I want to create a macro which selects and copies the last 10 rows of this data set (specifically the range from A to G ) and pastes them to another sheet.

Thanks in advance
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try:
Code:
Sub CopyLast10Rows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A" & LastRow - 9 & ":G" & LastRow).Copy Sheets("Sheet2").Cells(1, 1)
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
Code:
Sub CopyLast10Rows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A" & LastRow - 9 & ":G" & LastRow).Copy Sheets("Sheet2").Cells(1, 1)
    Application.ScreenUpdating = True
End Sub
I don't think you need those two Application.ScreenUpdating statements... copying does not display anything on the screen, so there is nothing taking place on the screen that needs to be suppressed... and since you are pasting to a non-displayed worksheet, there won't be any screen activity that will need to be suppressed there either.
 
Upvote 0
Thanks,

Mumps, the code works half way through I think. It selects the entire last row of the data set but not the last 10. Also it doesnt select the range from A go G but from A to the very last column with data. I think something small is missing.

Cheers.
 
Upvote 0
This will copy your data to Sheet Named "Sheet2"

Code:
Sub Test()
Application.ScreenUpdating = False
Dim i As Long
Dim Bottom As Integer
Dim Top As Integer
Bottom = Cells(Rows.Count, "A").End(xlUp).Row
Top = Bottom - 9
Range(Cells(Top, "A"), Cells(Bottom, "G")).Copy Destination:=Sheets("Sheet2").Range("A1")
Application.ScreenUpdating = True
End Sub

OK
 
Upvote 0
Thank you Rick, for picking up on that.

@mavara: I tried the code on some dummy data and it worked as you requested. This line of code:
Code:
Range("A" & LastRow - 9 & ":G" & LastRow)
copies from column A to column G not the entire row and it does copy only the last 10 lines. Your actual data may be organised in a different manner. The only way to be sure is if we could see your actual workbook. Perhaps you could upload a copy of your file to a free site such as www.box.com. or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Include a detailed explanation of what you would like to do referring to specific cells and worksheets. If the workbook contains confidential information, you could replace it with generic data.
 
Last edited:
Upvote 0
Mumps, My answer is this, thanks a lot guys - both solutions work perfectly fine.
Mumps, as I said I started practicing VBA recently so my ignorance made me wrongfully to conclude that the code was half way correct. Anyways all is clear now.

Thanks a lot again.
 
Upvote 0
Glad I was able to help you. Come back here to Mr. Excel next time you need additional assistance.
Mumps, My answer is this, thanks a lot guys - both solutions work perfectly fine.
Mumps, as I said I started practicing VBA recently so my ignorance made me wrongfully to conclude that the code was half way correct. Anyways all is clear now.

Thanks a lot again.
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,166
Members
448,870
Latest member
max_pedreira

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