VBA Copying a range from one workbook to another one

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

Trying to copy a range from one workbook to another one using the code below, but not sure what is wrong with the line:

.Range("A" & DestLastRow).Resize(.Rows.Count, .Columns.Count) = .Value


VBA Code:
Sub TPRP_Copy()
'Find the last used row in both sheets and copy and paste data below existing data.

Dim x As Workbook
Dim y As Workbook
Dim DestLastRow As Long

'## Open both workbooks first:
Set x = Workbooks.Open("S:\Everyone\Demand Planning\Primary & VE\Wow Daily Tracker\Source.xlsm")
Set y = Workbooks.Open("S:\Everyone\Demand Planning\Primary & VE\Wow Daily Tracker\TPRP History.xlsx")
  
     With Workbooks("TPRP History.xlsx").Worksheets("TPRP_History")
        DestLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
     End With
     
     'Transfer values from x to y:
     y.Sheets("TPRP_History").Range("A" & DestLastRow).Value = x.Sheets("Total_TPRP").Range("A16").CurrentRegion
   
     With Workbooks("TPRP History.xlsx").Worksheets("TPRP_History")
     
       .Range("A" & DestLastRow).Resize(.Rows.Count, .Columns.Count) = .Value
       
     End With
               
    'close the workbook and after saving
     Workbooks("TPRP History.xlsx").Close SaveChanges:=True
       
 End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Do you want to copy from workbook x to y or the other way around?
 
Upvote 0
How about
VBA Code:
Sub TPRP_Copy()
'Find the last used row in both sheets and copy and paste data below existing data.

Dim x As Workbook
Dim y As Workbook
Dim DestLastRow As Long

'## Open both workbooks first:
Set x = Workbooks.Open("S:\Everyone\Demand Planning\Primary & VE\Wow Daily Tracker\Source.xlsm")
Set y = Workbooks.Open("S:\Everyone\Demand Planning\Primary & VE\Wow Daily Tracker\TPRP History.xlsx")
 
     With Workbooks("TPRP History.xlsx").Worksheets("TPRP_History")
        DestLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
     End With
    
     'Transfer values from x to y:
     With x.Sheets("Total_TPRP").Range("A16").CurrentRegion
         y.Sheets("TPRP_History").Range("A" & DestLastRow + 1).Resize(.Rows.Count, .Columns.Count).Value = .Value
     End With
              
    'close the workbook and after saving
     Workbooks("TPRP History.xlsx").Close SaveChanges:=True
      
 End Sub
 
Upvote 0
How about
VBA Code:
Sub TPRP_Copy()
'Find the last used row in both sheets and copy and paste data below existing data.

Dim x As Workbook
Dim y As Workbook
Dim DestLastRow As Long

'## Open both workbooks first:
Set x = Workbooks.Open("S:\Everyone\Demand Planning\Primary & VE\Wow Daily Tracker\Source.xlsm")
Set y = Workbooks.Open("S:\Everyone\Demand Planning\Primary & VE\Wow Daily Tracker\TPRP History.xlsx")
 
     With Workbooks("TPRP History.xlsx").Worksheets("TPRP_History")
        DestLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
     End With
   
     'Transfer values from x to y:
     With x.Sheets("Total_TPRP").Range("A16").CurrentRegion
         y.Sheets("TPRP_History").Range("A" & DestLastRow + 1).Resize(.Rows.Count, .Columns.Count).Value = .Value
     End With
             
    'close the workbook and after saving
     Workbooks("TPRP History.xlsx").Close SaveChanges:=True
     
 End Sub
Thanks @Fluff, but not there yet, your code is taking the header as well when copying from source to target sheet, I tried the code below but still not working

VBA Code:
Sub TPRP_Copy()
'Find the last used row in both sheets and copy and paste data below existing data.

Dim x As Workbook
Dim y As Workbook
Dim DestLastRow As Long
Dim RngSource As Range


'## Open both workbooks first:
Set x = Workbooks.Open("S:\Everyone\Demand Planning\Primary & VE\Wow Daily Tracker\Source.xlsm")
Set y = Workbooks.Open("S:\Everyone\Demand Planning\Primary & VE\Wow Daily Tracker\TPRP History.xlsx")
  
     With Workbooks("TPRP History.xlsx").Worksheets("TPRP_History")
        DestLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
     End With
     
     'Transfer values from x to y:
     
     Set RngSource = x.Sheets("Total_TPRP").Range("A16").CurrentRegion
     Set RngSource = RngSource.Offset(1, 0)
     Set RngSource = RngSource.Resize(RngSource.Rows.Count - 1)
     
     y.Sheets("TPRP_History").Range("A" & DestLastRow).Value = RngSource
   
     'Transfer values from x to y:
     With x.Sheets("Total_TPRP").Range("A16").CurrentRegion
         y.Sheets("TPRP_History").Range("A" & DestLastRow + 1).Resize(.Rows.Count, .Columns.Count).Value = .Value
     End With
               
    'close the workbook and after saving
     Workbooks("TPRP History.xlsx").Close SaveChanges:=True
       
 End Sub
 
Upvote 0
If you don't want the header, try
VBA Code:
     'Transfer values from x to y:
     With x.Sheets("Total_TPRP").Range("A16").CurrentRegion.Offset(1)
         y.Sheets("TPRP_History").Range("A" & DestLastRow + 1).Resize(.Rows.Count, .Columns.Count).Value = .Value
     End With
 
Upvote 0
Solution
If you don't want the header, try
VBA Code:
     'Transfer values from x to y:
     With x.Sheets("Total_TPRP").Range("A16").CurrentRegion.Offset(1)
         y.Sheets("TPRP_History").Range("A" & DestLastRow + 1).Resize(.Rows.Count, .Columns.Count).Value = .Value
     End With
Thanks @Fluff!!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,902
Messages
6,122,161
Members
449,069
Latest member
msilva74

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