Copy/Paste between 2 VBA workbooks

Solved
ti_mouton Posted messages 153 Status Membre -  
pijaku Posted messages 13513 Registration date   Status Modérateur Last intervention   -
Bonjour,

I am new to VBA and I am looking to develop a macro that allows me to Copy/Paste the columns from the "Listing des Projets" sheet in the "Base de données" workbook to the "BDD" sheet of the workbook where I am developing the macro.
Thanks to the internet, I have managed to start writing the macro, but it doesn't work; I get an error message at this line:
classeurSource.Sheets("Listing des Projets").Cells.Copy classeurDestination.Sheets("BDD").Range("A1").Paste

Here is my entire macro:
 Sub CommandBouton1() Dim classeurSource As Workbook, classeurDestination As Workbook 'open the source workbook (read-only) Path = ThisWorkbook.Path Set classeurSource = Application.Workbooks.Open(Path & "\Base de données.xlsm") 'define the destination workbook Set classeurDestination = ThisWorkbook 'copy the data from the "Listing des Projets" sheet of the source workbook to the "BDD" sheet of the destination workbook classeurSource.Sheets("Listing des Projets").Cells.Copy classeurDestination.Sheets("BDD").Range("A1").Paste 'close the source workbook classeurSource.Close False End Sub


Thank you for your help.

1 réponse

pijaku Posted messages 13513 Registration date   Status Modérateur Last intervention   2 771
 
Hello,

Try it like this:

Sub CommandButton1() Dim sourceWorkbook As Workbook, destinationWorkbook As Workbook 'open the source workbook (read-only) Path = ThisWorkbook.Path 'define the destination workbook Set destinationWorkbook = ThisWorkbook Set sourceWorkbook = Application.Workbooks.Open(Path & "\Database.xlsm") 'copy data from the "Project Listing" sheet of the source workbook to the "DB" sheet of the destination workbook sourceWorkbook.Sheets("Project Listing").Cells.Copy destinationWorkbook.Sheets("DB").Range("A1") 'close the source workbook sourceWorkbook.Close False End Sub


EDIT: explanations:
I placed the definition of the destination workbook first since we were on ThisWorkbook.
What was causing the bug was the syntax with .Paste.
If you want to use .Paste, you need to put it on two lines of code:
Workbooks("thing").Sheets("Thing").Range("thing").Copy
Workbooks("destination").Sheets("sheet").Range("A1").Paste


Without Paste, on a single line of code:
sourceWorkbook.Sheets("Project Listing").Cells.Copy destinationWorkbook.Sheets("DB").Range("A1")

🎼 Best regards,
Franck 🎶
2
ti_mouton Posted messages 153 Status Membre
 
Perfect, it works very well! A big THANK YOU for your valuable help and your quick response.
0
pijaku Posted messages 13513 Registration date   Status Modérateur Last intervention   2 771 > ti_mouton Posted messages 153 Status Membre
 
You're welcome.
See you!
0