任何人都可以帮助使用这个Excel宏吗?我试图从一张纸复制到另一张,但它在
崩溃了Sheets("Sheet1").Range("A6").PasteSpecial xlPasteValues
不知道我做错了什么......
Sub Copy()
Sheets("Sheet1").Range("A2:D26").Copy
Workbooks.Open("C:\Users\j\Trial.xltx").Activate
Sheets("Sheet1").Range("A6").PasteSpecial xlPasteValues
Application.CutCopyMode = False
rngDest.Insert xlShiftDown
Err_Execute:
If Err.Number = 0 Then MsgBox "Copying Successful :)" Else _
MsgBox Err.Description
End Sub
这也是粘贴值和插入数据的最佳方法吗? 感谢
答案 0 :(得分:1)
尝试为所有对象设置父级,并直接与他们合作,远离.Activate
语句。试试这个并确保Trail.xltx
工作簿实际上有一个名为Sheet1
的工作表。
Sub Copy()
Dim wbThis as Workbook
Set wbThis = ThisWorkbook
wbThis.Sheets("Sheet1").Range("A2:D26").Copy
Dim wbPaste as Workbook
Set wbPaste = Workbooks.Open("C:\Users\j\Trial.xltx")
wbPaste.Sheets("Sheet1").Range("A6").PasteSpecial xlPasteValues
rngDest.Insert xlShiftDown ' this is bizarre, because you never set this variable
Err_Execute:
If Err.Number = 0 Then MsgBox "Copying Successful :)" Else _
MsgBox Err.Description
End Sub