I would like to open a workbook and copy from the opened workbooks sheet to my worbook sheet like that:
Private Sub Workbook_Open()
Dim openedFile As String
Dim sourcebook As Workbook
openedFile = Application.GetOpenFilename(fileFilter:="Excel Macro-Enabled Workbook (*.xlsm), *.xlsm") 'Source book opening
Set sourcebook = Workbooks.Open(openedFile)
Application.CutCopyMode = True
sourcebook.Worksheets("source_sheet").Range("A1:L100").Copy
ThisWorkbook.Worksheets("dest_sgheet").Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Call sourcebook.Close(False)
End Sub
I got this error message after I runing this code:
Run-time error '1004": PasteSpecial method of Range class failed
It's occour at this line:
ThisWorkbook.Worksheets("dest_sheet").Range("A1").PasteSpecial Paste:=xlPasteValues
try activating the workbook first ("This workbook") and then activating the sheet where you'd like to paste the data. The error tends to occur because either the range is not visible nor in existence.
Your code should work . change this line:
ThisWorkbook.Worksheets("dest_sgheet").Range("A1").PasteSpecial Paste:=xlPasteValues
with this one:
ThisWorkbook.Worksheets("dest_sheet").Range("A1").PasteSpecial Paste:=xlPasteValues
EDIT Based on OP comments on 25-05-2016 I have re-run the program as per OP code after correcting minor typo error in sheet spelling. Program runs successfully without any problem. I am not getting Run time error 1004. Screenshot shows that data is getting copied 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.