Copy last 5 rows in range (“A:K”) from one open workbook and paste into another closed workbook.

ProgramUser

Board Regular
Joined
Apr 15, 2014
Messages
75
Currently using W10, Excel Pro 2013 (Both 64Bit)

Dear Excellers!!!!!
With only a few hairs left upon my head, and after ‘days!’ of searching and trying to work out ‘why’ I’m getting silly errors with my code….I come to you for your assistance.

What I have thus far;
I have two workbooks, Book1.xlsm and Book2.xlsm
Book1 contains references to products with some reporting guidelines via data validation etc (trivial) located in range Columns A through to Column K and the list is dynamic and regularly is appended to;

I can already achieve this via my own working code;
Within Book1 - I take the information from sheet tab “Source1”, copy it to sheet tab “Sorted”, then sort it via another macro (this works well)

Requirement;

So…
Book2 (would be closed at this point) and is the book that I need to copy data to, from Book1 (which is open). The copy process involves taking a range (found at book1.xlsm, sheet “Sorted”), taking the last 5 rows from that found range, then pasting it into book2 into sheet named “Vendor_List_Report”.
Where I’m falling over is ascertaining the ‘range’ to Search through (A:K). For Example;

Code:
Set r1Last5Rows = wsFrom.Range("A" & r1LastRow - 5 & ":K" & r1LastRow)

I have my code attached but am constantly receiving a runtime error ‘13’, type mismatch. I have tried numerous methods but if I manage to get another code approach to work, then I can’t get the loop to work (not seen here), or if the looping code is good, I can’t get past error 423 workbook cannot be found or similar….very frustrating.
I am showing you the type of code I’ve modified rather than all the code snippets I’ve attempted.
Ideally, it would be nice to have the code assess to see if Book2 was open (workbook.open = No) or not first, if not open, then open it, perform the copy/paste, then close book2. More importantly, it’s the last 5 rows that are crucial to the end-game here.
If you feel that I’d be better to look at another post that I may have missed, then please suggest this….if not and you can assist me directly, then I’d be just as happy for someone to edit my code accordingly. Again, you never stop learning however, sometimes you can’t see the forest for the trees.
Begin Book1 HTML
(sheet tab – “Source1”)
Excel 2012
ABCDEFGHIJK
DDR3G.SkillBelow 0Res.NetworkToday
DDR4CorsairBelow 0Res.NetworkToday

<tbody>
[TD="align: center"]1[/TD]
[TD="align: center"]SKU[/TD]
[TD="align: center"]RAMTYPE[/TD]
[TD="align: center"]Freq[/TD]
[TD="align: center"]Socket[/TD]
[TD="align: center"]Brand[/TD]
[TD="align: center"]Fail_Rate[/TD]
[TD="align: center"]Temp[/TD]
[TD="align: center"]Rail_Voltage[/TD]
[TD="align: center"]Installation[/TD]
[TD="align: center"]Network_Service[/TD]
[TD="align: center"]Date_Last_Visited[/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]1110838[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1333[/TD]

[TD="align: right"]101[/TD]

[TD="align: right"]1.1[/TD]

[TD="align: center"]3[/TD]
[TD="align: right"]1110983[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]2400[/TD]

[TD="align: right"]101[/TD]

[TD="align: right"]1.1[/TD]

</tbody>
Source1

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR="bgcolor: #DAE7F5"]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]B2[/TH]
[TD="align: left"]=IFERROR(VLOOKUP($A2,Product_Listing!$A$3:$B$31,2,FALSE),"")[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]C2[/TH]
[TD="align: left"]=INDEX(Product_Listing!$D$4:$D$7,MATCH(Source1!$B2,Product_Listing!$C$4:$C$7))[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]D2[/TH]
[TD="align: left"]=INDEX(Product_Listing!$E$4:$E$7,MATCH(Source1!$C2,Product_Listing!$D$4:$D$7))[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]E2[/TH]
[TD="align: left"]=INDEX(Product_Listing!$F$4:$F$7,MATCH(Source1!$D2,Product_Listing!$E$4:$E$7))[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]B3[/TH]
[TD="align: left"]=IFERROR(VLOOKUP($A3,Product_Listing!$A$3:$B$31,2,FALSE),"")[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]C3[/TH]
[TD="align: left"]=INDEX(Product_Listing!$D$4:$D$7,MATCH(Source1!$B3,Product_Listing!$C$4:$C$7))[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]D3[/TH]
[TD="align: left"]=INDEX(Product_Listing!$E$4:$E$7,MATCH(Source1!$C3,Product_Listing!$D$4:$D$7))[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]E3[/TH]
[TD="align: left"]=INDEX(Product_Listing!$F$4:$F$7,MATCH(Source1!$D3,Product_Listing!$E$4:$E$7))[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]G2[/TH]
[TD="align: left"]=INDEX(Product_Listing!$H$3:$H$6,MATCH(Source1!$F2,Product_Listing!$G$3:$G$6))[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]H2[/TH]
[TD="align: left"]=IF($G2="Hot",Product_Listing!$I$6,INDEX(Product_Listing!$I$3:$I$6,MATCH(Source1!$G2,Product_Listing!$H$3:$H$6)))[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]G3[/TH]
[TD="align: left"]=INDEX(Product_Listing!$H$3:$H$6,MATCH(Source1!$F3,Product_Listing!$G$3:$G$6))[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]H3[/TH]
[TD="align: left"]=IF($G3="Hot",Product_Listing!$I$6,INDEX(Product_Listing!$I$3:$I$6,MATCH(Source1!$G3,Product_Listing!$H$3:$H$6)))[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]


END HTML – Book1-Source1
Begin Book1 HTML
(sheet tab – “Sorted”)
Excel 2012
ABCDEFGHIJK
SKURAMTYPEFreqSocketBrandFail_RateTempRail_VoltageInstallationNetwork_ServiceDate_Last_Visited
DDR4CorsairBelow 0Res.NetworkToday
DDR4CorsairHotMachineHouseYesterday

<tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]1110983[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]2400[/TD]

[TD="align: right"]101[/TD]

[TD="align: right"]1.1[/TD]

[TD="align: center"]3[/TD]
[TD="align: right"]1111024[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]2000[/TD]

[TD="align: right"]104[/TD]

[TD="align: right"]2.15[/TD]

</tbody>
Sorted

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<tbody>[TR="bgcolor: #DAE7F5"]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]A1[/TH]
[TD="align: left"]SKU[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]B1[/TH]
[TD="align: left"]RAMTYPE[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]C1[/TH]
[TD="align: left"]Freq[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]D1[/TH]
[TD="align: left"]Socket[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]E1[/TH]
[TD="align: left"]Brand[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]F1[/TH]
[TD="align: left"]Fail_Rate[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]G1[/TH]
[TD="align: left"]Temp[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]H1[/TH]
[TD="align: left"]Rail_Voltage[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]I1[/TH]
[TD="align: left"]Installation[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]J1[/TH]
[TD="align: left"]Network_Service[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]K1[/TH]
[TD="align: left"]Date_Last_Visited[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]A2[/TH]
[TD="align: left"]1110983[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]B2[/TH]
[TD="align: left"]4[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]C2[/TH]
[TD="align: left"]2400[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]D2[/TH]
[TD="align: left"]DDR4[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]E2[/TH]
[TD="align: left"]Corsair[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]F2[/TH]
[TD="align: left"]101[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]G2[/TH]
[TD="align: left"]Below 0[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]H2[/TH]
[TD="align: left"]1.1[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]I2[/TH]
[TD="align: left"]Res.[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]J2[/TH]
[TD="align: left"]Network[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]K2[/TH]
[TD="align: left"]Today[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]A3[/TH]
[TD="align: left"]1111024[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]B3[/TH]
[TD="align: left"]3[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]C3[/TH]
[TD="align: left"]2000[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]D3[/TH]
[TD="align: left"]DDR4[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]E3[/TH]
[TD="align: left"]Corsair[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]F3[/TH]
[TD="align: left"]104[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]G3[/TH]
[TD="align: left"]Hot[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]H3[/TH]
[TD="align: left"]2.15[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]I3[/TH]
[TD="align: left"]Machine[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]J3[/TH]
[TD="align: left"]House[/TD]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]K3[/TH]
[TD="align: left"]Yesterday[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]


End HTML – Book1-Sorted
Begin Code
Code:
' Credits go to Duly noted authors below
' Inspiration from: From https://stackoverflow.com/questions/38693039/selecting-last-8-rows-in-excel-vba
' &
' Inspiration from: http://www.vbaexpress.com/forum/archive/index.php/t-45117.html
' &
' Inspiration from: https://stackoverflow.com/questions/38693039/selecting-last-8-rows-in-excel-vba/38693171
 
Sub CopyLast5Rowstest()
    Dim wsFrom As Worksheet, wsTo As Worksheet
    
    Set wsFrom = Workbooks("Book1.xlsm").Worksheets("Sorted") ' Copying from here
    Set wsTo = Workbooks("Book2.xlsm").Worksheets("Vendor_List_Report") ' pasting to here
    
    Call CopyPaste(wsFrom.Range("A:A"), wsTo.Range("A:A"))
    Call CopyPaste(wsFrom.Range("B:B"), wsTo.Range("B:B"))
    Call CopyPaste(wsFrom.Range("C:C"), wsTo.Range("C:C"))
    Call CopyPaste(wsFrom.Range("D:D"), wsTo.Range("D:D"))
    Call CopyPaste(wsFrom.Range("E:E"), wsTo.Range("E:E"))
    Call CopyPaste(wsFrom.Range("F:F"), wsTo.Range("F:F"))
    Call CopyPaste(wsFrom.Range("G:G"), wsTo.Range("G:G"))
    Call CopyPaste(wsFrom.Range("H:H"), wsTo.Range("H:H"))
    Call CopyPaste(wsFrom.Range("I:I"), wsTo.Range("I:I"))
    Call CopyPaste(wsFrom.Range("J:J"), wsTo.Range("J:J"))
    Call CopyPaste(wsFrom.Range("K:K"), wsTo.Range("K:K"))
    'Call CopyPaste(wsFrom.Range("L:L"), wsTo.Range("L:L"))
 
End Sub
 
Sub CopyPaste(RangePaste As Range, wsFrom As Workbook)
    Dim r1LastRow As Range, r2 As Range, r1Last5Rows As Range
    Set wsFrom = Workbook
    Set r1LastRow = wsFrom.Range("A" & wsFrom.Rows.Count).End(xlUp) ' from this sheet range
    Set r1Last5Rows = wsFrom.Range("A" & r1LastRow - 5 & ":K" & r1LastRow) 'from this sheet range
    
    Set r2 = RangePaste.Cells(2, 1) ' paste to
    r1Last5Rows.Copy
    r2.PasteSpecial Paste:=xlPasteValues ' paste to
    
    Application.CutCopyMode = xlCopy
    
End Sub

Thanks in advance.
ProgramUser
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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