Excel VBA:将其他工作簿中的内容粘贴到活动工作簿Sheet2中

时间:2018-09-06 12:49:47

标签: excel vba excel-vba

正如标题所述,以下是我尝试(在我的活动工作簿中)打开另一个工作簿,复制其内容,然后将其粘贴到当前工作簿的第二张纸中。但是我无法通过粘贴动作。我一直无法在线找到与此特定案例相关的示例。

Sub SL()
Dim x As Workbook

Set x = Workbooks.Open("C:\Stuff.xlsx")

x.Sheets("SheetName").Cells.Copy

ActiveWorkbook.Sheets("Sheet2").Cells.PasteSpecial

End Sub

请问有人可以解释一下我应该在里面放什么吗?

1 个答案:

答案 0 :(得分:1)

我相信以下内容将帮助您实现您的期望:

Sub SL()
Dim x As Workbook

Set x = Workbooks.Open("C:\Stuff.xlsx")

x.Sheets("SheetName").Cells.Copy
'does your workbook actually have a Sheet named "SheetName"?
'if not, you need to reference the sheet that you want to copy

ThisWorkbook.Sheets("Sheet2").Cells.PasteSpecial

x.Close False
'close your "Stuff" workbook without saving
End Sub