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 :eek:)


Many thanks in advance
 

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
5,073
Office Version
2019
Platform
Windows
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")
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,362
Office Version
2013
Platform
Windows
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")
 

geojowett

New Member
Joined
Jul 16, 2017
Messages
9
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?
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,362
Office Version
2013
Platform
Windows
Why do you have different range: Range("A1:E100") & ("A1:A100")?
 

geojowett

New Member
Joined
Jul 16, 2017
Messages
9
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?
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,362
Office Version
2013
Platform
Windows
Try:
wbTarget.Sheets("BCH1").Range("A1:E100").Value = wbSource.Sheets("BCH").Range("A1:E100").Value
 
Last edited:

geojowett

New Member
Joined
Jul 16, 2017
Messages
9
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
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,362
Office Version
2013
Platform
Windows
You're welcome & thanks for replying.
Since it’s different from your initial problem, you may want to start a new thread.
 

Forum statistics

Threads
1,082,139
Messages
5,363,363
Members
400,731
Latest member
Jackserver

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top