Tranfser data between two closed workbooks without creating a new one

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Tranfser data between two closed workbooks without creating a new one



how do I tranfser data between two closed workbooks without creating a new one? I have the following scenario:



My question: Is it possible to activate a macro from a seperate excel workbook - lets name it wb3? So, triggering the macro in wb3 would transfer all the relevant data from wb1 to wb2....how would a macro like this look like?



Thanks!


Sub CommandButton1_Click()
Dim wb1 As Workbook
Dim wb2 As Workbook

Set wb1 = Application.workbooks.open("C:UsersPlutoXMyExcelfile1.xlsx")
Set wb2 = Application.workbooks.open("C:UsersPlutoXMyExcelfile1.xlsx")


wb2.Sheets("Sheet1").Range("A1") = wb1.Sheets("Sheet1").Range("A1")



wb1.Close False
wb2.Close True

End Sub





You can via VBA open both worbooks, select all the data you need (therefor you need to know where everything is stored) and transfer your data to the second one. Workbook1.Sheets("Sheet1").Range("A1") = Workbook2.Sheets("Sheet1").Range("A1"). Something like this, but ofc a little more structured etc. After transfering data you can let the code close both of these workbooks
– Lutscha
Aug 8 at 7:52





@plutoX: Welcome to Stack Overflow: Please take the tour and read How to ask a good question, then edit your question to include the code, expected behaviour, and what is wrong... then we can try to help
– Our Man in Bananas
Aug 8 at 8:28






@plutox: please show us what you have tried
– Our Man in Bananas
Aug 8 at 8:28






Thanks for the feedback! As I'm a complete excel noob - would the code look something like this? I've updated my original post with the code snipped. Thanks so much guys..
– PlutoX
Aug 8 at 8:34






@PlutoX: thank you - I have removed my downvote, and retracted my close vote. If the answer provided by Alex solved your problem, please Accept it by clicking the green check/tick mark. If you need further help, put a comment to Alex under his answer.
– Our Man in Bananas
Aug 8 at 10:24




1 Answer
1



Almost there! You need to change three things:



If you want to open a workbook, you need to state the full path


Set wb1 = Application.workbooks.open("C:UsersPlutoXMyExcelfile1.xlsx")



If stuff needs to be transferred to wb2, the order is wb2 = wb1 so


wb2.sheets("Sheet1").Range("A1").value = wb1.sheets("Sheet1").Range("A1").value



If you close the workbooks, make sure you save at least the one with changes


wb2.Close True





Thanks so much! I've updated my code snipped according to your feedback, and added it to a button in wb3 - but I get an error message stating: "invalid outside of a procedure". Have I missed something?
– PlutoX
Aug 8 at 10:45






...the "Sub CommandButton1_Click()" part of the code is highlighted in yellow as well - so I guess that this part of the code is somehow tied to the error message.
– PlutoX
Aug 8 at 10:54





I've tried it again - it worked!! thanks so much for the help guys!
– PlutoX
Aug 8 at 11:07






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS