copy data from one workbook to another

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,259
Office Version
  1. 2010
Platform
  1. Windows
Hi I have the code below where I am trying to copy a range A1 to G15 into another workbook but its not working please can you help. I am trying to copy from Daily stats into Daily Stats WC Template, hope you can advise?
Code:
Private Sub CommandButton1_Click()
Dim x As Workbook
Dim y As Workbook
Dim vals As Variant
Set x = ThisWorkbook
Set y = Workbooks.Open("G:Daily Stats WC Template.xlsx")
With x.Sheets("Daily Stats")
   Intersect(.UsedRange, .Range("A1:G15").Copy y.Sheets("Daily Stats").Range("A1")
End With
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Can you please repost the code with the amendment?
 
Upvote 0
Code:
Sub Open_Workbook()
 
    Dim srcWB As Workbook
    Dim destWB As Workbook
    Dim fName As String
    Dim lastRow As Long
 
'   Capture current workbook as source workbook
    Set srcWB = ActiveWorkbook
 
'   Open destination workbook and capture it as destination workbook
    Workbooks.Open "G:\Daily Stats WC Template.xlxs”
    Set destWB = ActiveWorkbook
  
  

'   Copy data from source workbook to destination workbook
srcWB.Sheets("Daily Stats").Range("A1:A15").Copy
    destWB.Sheets("Template").Range("A1").Resize(15, 1).PasteSpecial xlPasteValues
srcWB.Sheets("Daily Stats").Range("B1:B15").Copy
    destWB.Sheets("Template").Range("B1").Resize(15, 1).PasteSpecial xlPasteValues
srcWB.Sheets("Daily Stats").Range("C1:C15").Copy
    destWB.Sheets("Template").Range("C1").Resize(15, 1).PasteSpecial xlPasteValues
srcWB.Sheets("Daily Stats").Range("D1:D15").Copy
    destWB.Sheets("Template").Range("D1").Resize(15, 1).PasteSpecial xlPasteValues
srcWB.Sheets("Daily Stats").Range("E1:E15").Copy
    destWB.Sheets("Template").Range("E1").Resize(15, 1).PasteSpecial xlPasteValues
srcWB.Sheets("Daily Stats").Range("F1:F15").Copy
    destWB.Sheets("Template").Range("F1").Resize(15, 1).PasteSpecial xlPasteValues
srcWB.Sheets("Daily Stats").Range("G1:G15").Copy
    destWB.Sheets("Template").Range("G1").Resize(15, 1).PasteSpecial xlPasteValues

'   Save changes and close destination workbook
    destWB.Close SaveChanges:=True
End Sub

If his fix doesn't work, this should do it
 
Last edited:
Upvote 0
I just realized I left the last row part on there. My vba was set to enter into a table and keep data. It doesn't appear that's what you want. So i just edited the last code I posted to take out the last row portion. It should automatically paste over any data in those ranges now. The way it was before would post under data in those ranges
 
Upvote 0
I just realized I left the last row part on there. My vba was set to enter into a table and keep data. It doesn't appear that's what you want. So i just edited the last code I posted to take out the last row portion. It should automatically paste over any data in those ranges now. The way it was before would post under data in those ranges

Thank you I shall try this in the morning :)
 
Upvote 0
Hi thank you for this it works great :) please can you advise how can it automoatically open the daily stats wc template nstead of loading it manually? thanks again much appreciated
 
Upvote 0
If the file path is correct in the open destination workbook part of the code, you just have to start running the code and it will do everything for you.

That file path tells it where it’s at, and you’re making it open. If you have to open and copy over stuff in a lot of workbooks it’s a very useful bit of code to include
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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