Need to copy last n rows of range to 2nd worksheet

kpm30519

New Member
Joined
Nov 9, 2018
Messages
22
Hi--

Major senior moment-- I need a macro to copy the last n rows of a range (A:I) to another worksheet. It needs to be dynamic (might be last 10, might be last 27, etc.) On the worksheet the data goes to, it should always paste to A3 so it will update/ overwrite the previous data.

Ex:
Today, I have data in Sheet1(A3:I300). I need Sheet1(A290:I300) copied to Sheet2(A3).
Tomorrow, I will have data in Sheet1(A3:I425), but I need Sheet1(A415:I425) copied to Sheet2(A3).

Hope this makes sense!

Thanks in advance.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Am I reading you right that you want the last 11 used rows copied and pasted to another sheet?
First you say that it might be a different amount and after that your examples show just 11 on both occasions.
How are we to know how many?
 
Upvote 0
If you always have the range to copy ending with the last row of data and you are designating the starting row manually then
Code:
With ActiveSheet
 Application.DisplayAlerts = False
 .Range(.Cells(ActiveCell.Row, 1), .Cells(Rows.Count, 1).End(xlUp)).Resize(, 9).Copy Sheets(2).Range("A3")
 Application.DisplayAlerts = True
End With
If you are finding the starting row by code then the criteria would determine how the copy range is formed.
 
Last edited:
Upvote 0
try PowerQuery

M-code for result table (Table1):

Code:
[SIZE=1]// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Counter = Table.FirstValue(Excel.CurrentWorkbook(){[Name="Table2"]}[Content]),
    Type = Table.TransformColumnTypes(Source,{{"Date", type date}, {"ID", type text}, {"Name", type text}, {"Company", type text}, {"Country", type text}, {"City", type text}, {"Sales", Int64.Type}}),
    LastN = Table.LastN(Type,Counter)
in
    LastN[/SIZE]

and small single row table (Table2) where you can determine how many last rows you want to show/copy

[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td=bgcolor:#5B9BD5]LastN[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#DDEBF7]
12​
[/td][/tr]
[/table]


example:

screenshot-95.png
 
Last edited:
Upvote 0
Code:
Sub Maybe()
Dim i As Long, sh1 As Worksheet, sh2 As Worksheet
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
sh2.UsedRange.Offset(2).ClearContents
i = InputBox("How many rows do want to copy?", "Amount of rows.")
    With sh1
        .Cells(.Rows.Count, 1).End(xlUp).Offset(-(i-1)).Resize(i, 9).Copy sh2.Range("A3")
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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