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
1SKURAMTYPEFreqSocketBrandFail_RateTempRail_VoltageInstallationNetwork_ServiceDate_Last_Visited
2111083821333DDR3G.Skill101Below 01.1Res.NetworkToday
3111098342400DDR4Corsair101Below 01.1Res.NetworkToday

<tbody>
</tbody>
Source1

Worksheet Formulas
CellFormula
B2=IFERROR(VLOOKUP($A2,Product_Listing!$A$3:$B$31,2,FALSE),"")
C2=INDEX(Product_Listing!$D$4:$D$7,MATCH(Source1!$B2,Product_Listing!$C$4:$C$7))
D2=INDEX(Product_Listing!$E$4:$E$7,MATCH(Source1!$C2,Product_Listing!$D$4:$D$7))
E2=INDEX(Product_Listing!$F$4:$F$7,MATCH(Source1!$D2,Product_Listing!$E$4:$E$7))
B3=IFERROR(VLOOKUP($A3,Product_Listing!$A$3:$B$31,2,FALSE),"")
C3=INDEX(Product_Listing!$D$4:$D$7,MATCH(Source1!$B3,Product_Listing!$C$4:$C$7))
D3=INDEX(Product_Listing!$E$4:$E$7,MATCH(Source1!$C3,Product_Listing!$D$4:$D$7))
E3=INDEX(Product_Listing!$F$4:$F$7,MATCH(Source1!$D3,Product_Listing!$E$4:$E$7))
G2=INDEX(Product_Listing!$H$3:$H$6,MATCH(Source1!$F2,Product_Listing!$G$3:$G$6))
H2=IF($G2="Hot",Product_Listing!$I$6,INDEX(Product_Listing!$I$3:$I$6,MATCH(Source1!$G2,Product_Listing!$H$3:$H$6)))
G3=INDEX(Product_Listing!$H$3:$H$6,MATCH(Source1!$F3,Product_Listing!$G$3:$G$6))
H3=IF($G3="Hot",Product_Listing!$I$6,INDEX(Product_Listing!$I$3:$I$6,MATCH(Source1!$G3,Product_Listing!$H$3:$H$6)))

<tbody>
</tbody>

<tbody>
</tbody>


END HTML – Book1-Source1
Begin Book1 HTML
(sheet tab – “Sorted”)
Excel 2012
ABCDEFGHIJK
1SKURAMTYPEFreqSocketBrandFail_RateTempRail_VoltageInstallationNetwork_ServiceDate_Last_Visited
2111098342400DDR4Corsair101Below 01.1Res.NetworkToday
3111102432000DDR4Corsair104Hot2.15MachineHouseYesterday

<tbody>
</tbody>
Sorted

Worksheet Formulas
CellFormula
A1SKU
B1RAMTYPE
C1Freq
D1Socket
E1Brand
F1Fail_Rate
G1Temp
H1Rail_Voltage
I1Installation
J1Network_Service
K1Date_Last_Visited
A21110983
B24
C22400
D2DDR4
E2Corsair
F2101
G2Below 0
H21.1
I2Res.
J2Network
K2Today
A31111024
B33
C32000
D3DDR4
E3Corsair
F3104
G3Hot
H32.15
I3Machine
J3House
K3Yesterday

<tbody>
</tbody>

<tbody>
</tbody>


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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,217,448
Messages
6,136,682
Members
450,024
Latest member
Beagle263

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