This code just opens the source file but doesn't copy anything from it. I want to copy the data from my source file and paste it to the destination file.
Sub copy()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("Source File")
Set y = Workbooks.Open("Destination File")
y.Sheets("Sheet1").Range("A1").Value = x.Sheets("Sheet1").Range("A1")
x.Close
End Sub
Try to debug, doing the following:
Sub Copy()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("Source File")
Set y = Workbooks.Open("Destination File")
Debug.Print x.Name
Debug.Print y.Name
Debug.Print x.Sheets("Sheet1").Range("A1").value
Debug.Print y.Sheets("Sheet1").Range("A1")
Debug.Print x.Sheets("Sheet1").Range("A1").Address
Debug.Print y.Sheets("Sheet1").Range("A1").Address
End Sub
You are missing .Value
ant the end of your line:
y.Sheets("Sheet1").Range("A1").Value = x.Sheets("Sheet1").Range("A1").Value
Or Try:
y.Sheets(1).Cells(1, 1).Value = x.Sheets(1).Cells(1, 1).Value
I guess you don't have Sheet1 in both workbook, so lets try different notation.
Try debugging your code with:
msgbox y.Sheets("Sheet1").Range("A1").Value
msgbox x.Sheets("Sheet1").Range("A1").Value
I guess your files are not being opened properly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.