Forum Discussion
Reza_Majdmrmajd
Dec 12, 2023Copper Contributor
Data transfering from one workbook to another using VBA code
Hello everyone. I have an issue with below code. it make the new sheet with the name that i choose from a cell but the data does not transfer from the source file that I already selected. try many d...
djclements
Dec 13, 2023Bronze Contributor
Reza_Majdmrmajd After analyzing your code, there are a number of things I would do differently. Having said that, the problem you're experiencing is a result of not directly identifying which workbook the "newSheet" is referring to. When you use:
Set newSheet = Worksheets(ClientName)
and:
Set newSheet = Worksheets.Add
...the action is being perform on the "ActiveWorkbook", which at this point in the procedure is the source workbook "wb". Since you later use:
' Close the source workbook
wb.Close SaveChanges:=False
...to close "wb" without saving changes, I'm assuming that it was your intention to copy the source data to a new sheet in the current workbook. To do so, you need to add "ThisWorkbook" to each line:
Set newSheet = ThisWorkbook.Worksheets(ClientName)
and:
Set newSheet = ThisWorkbook.Worksheets.Add
I hope that helps. Cheers!