Hi all,
Please note that I am trying to copy data from a workbook to another workbook. What I am trying to achieve is that the data gets pasted to the last cell of the destination workbook.
For that I had found a code in the web that does it, however, the source and destination excel files are located in different paths and I am not being able to reference each one of them to the correct path, it continues to return error. Do you know how can I improve the below code to do that? Thanks!
Thanks!
Please note that I am trying to copy data from a workbook to another workbook. What I am trying to achieve is that the data gets pasted to the last cell of the destination workbook.
For that I had found a code in the web that does it, however, the source and destination excel files are located in different paths and I am not being able to reference each one of them to the correct path, it continues to return error. Do you know how can I improve the below code to do that? Thanks!
Code:
Private Sub CommandButton3_Click()Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'Set variables for copy and destination sheets
Set wsCopy = Workbooks("Source.xlsm").Worksheets("Selection")
Set wsDest = Workbooks("Destination.xlsx").Worksheets("Sheet1")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
'3. Copy & Paste Data
wsCopy.Range("A2:K" & lCopyLastRow).Copy _
wsDest.Range("A" & lDestLastRow)
End Sub
Thanks!