VBA copy data to another file

geojowett

New Member
Joined
Jul 16, 2017
Messages
9
Hi there,

I am trying to copy some data a file called "Excel Macro.xlsm" to a file called "BCH.xlsx" but cnt seem to manage it.

The original file "Excel Macro.xlsm" contains 2 sheets: master and BCH. I have placed a button in the 'master' sheet which should extract cells A1:E20 from the BCH sheet (in Excel Macro.xlsm) and place it in an unopen Excel file called "BCH.xlsx" which contains just one sheet "BCH".

The code I'm using is as follows:

Sub Demo()
Dim wbSource As Workbook
Dim wbTarget As Workbook

Set wbSource = Workbooks.Open(“c:\Users\Chris\desktop\Excel Macro.xlsm”)
Set wbTarget = Workbooks.Open("c:\Users\Chris\desktop\BCH.xlsx") ' Workbooks.Open(" ")

wbTarget.Sheets("BCH").Range("A1:E20").Value = wbSource.Sheets("BCH").Range("A1:E20")
wbSource.Close
End Sub


Can anyone point me in the right direction - I get a "Syntax Error. Compile error" error message when I run it.
This is the first time Ive tried to do VBA so go easy :o)


Many thanks in advance
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I don't get an error message, but nothing moves. I changed it up to copy from Source and paste to the Target and that works find for me.

change your sheet names
Code:
wbSource.Sheets("Sheet1").Range("A1:E20").Copy wbTarget.Sheets("Sheet1").Range("A1")
 
Upvote 0
Do you notice that the double quote (“) in this line is different with what it should be ("):
Code:
Set wbSource = Workbooks.Open(“c:\Users\Chris\desktop\Excel Macro.xlsm”)
Try changing to this:
Code:
Set wbSource = Workbooks.Open("c:\Users\Chris\desktop\Excel Macro.xlsm")
 
Upvote 0
Thanks guys,

Renamed the sheets and changed the quote marks and now the BCH file opens but doesnt populate with the data fomr the Excel Macro.

This is the current code:

Private Sub CommandButton2_Click()




Dim wbSource As Workbook
Dim wbTarget As Workbook




Set wbSource = Workbooks.Open("c:\Users\c.jowett.STARDOMAIN\desktop\Excel Macro.xlsm")
Set wbTarget = Workbooks.Open("c:\Users\c.jowett.STARDOMAIN\desktop\BCH.xlsx") ' Workbooks.Open(" ") ' <<< path to destination workbook


'Now, transfer values from wbSource to wbTarget:
wbTarget.Sheets("BCH1").Range("A1:E100").Value = wbSource.Sheets("BCH").Range("A1:A100")


wbSource.Close


End Sub



Any more suggestions at all?
 
Upvote 0
Why do you have different range: Range("A1:E100") & ("A1:A100")?
 
Upvote 0
Why do you have different range: Range("A1:E100") & ("A1:A100")?

Good point! well spotted. I have changed them both to A1:E100 but this still just opens the BCH document and leaves it blank with no data copied.


In the following line:

wbTarget.Sheets("BCH1").Range("A1:E100").Value = wbSource.Sheets("BCH").Range("A1:A100")

The BCH1 sheet (in BCH.xlsx) is the target sheet and the BCH sheet (in Excel Macro.xlsm) is the source- is this coded correctly for this? or should they both be BCH1?
 
Upvote 0
Try:
wbTarget.Sheets("BCH1").Range("A1:E100").Value = wbSource.Sheets("BCH").Range("A1:E100").Value
 
Last edited:
Upvote 0
Try:
wbTarget.Sheets("BCH1").Range("A1:E100").Value = wbSource.Sheets("BCH").Range("A1:E100").Value


It worked! fantastic many thanks Akuini. Thats made my day!

Can I pick you brains about my next task please (or does this need a new thread?)

Now the data from the 'BCH' sheet in in "Excel Macro.xslm" is copying to a new sheet called 'BCH' - how could I make another sheet in "Excel Macro.xslm" called 'TEST' copy the data from this sheet to a new file called "TEST.xlsx"?

The idea is that one sheet can contain the main data but with the click of just one button it can transfer the data from each of the sheets into new files. ie BCH sheet copies to a new file called BCH, TEST sheet copies to a new file called TEST.xslx and TEST2 sheet will copy to a new file called TEST2.xslx

I realise I could do this with multiple buttons using the same code as above, but I would like this one button to open and copy data to multiple sheets - is this easy to do?

Many thanks again
 
Upvote 0
You're welcome & thanks for replying.
Since it’s different from your initial problem, you may want to start a new thread.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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