我一直在搜索超过一个小时,找不到任何适用于此的内容,并会感激任何帮助。我一直在使用以下代码:
Sub copySheet()
Dim srcBook As Workbook
Set srcBook = Application.Workbooks.Open(ThisWorkbook.Path & "\Book1.xlsx")
srcBook.Sheets("Sheet1").Copy After:=ThisWorkbook.Sheets(1)
srcBook.Close False
End Sub
此按预期复制,但在复制的工作簿中,它会创建一个新工作表" Sheet1(2)"而不是将其添加到现有的sheet1。如果重复,它会创建" Sheet1(3)"," Sheet1(4)"," Sheet1(5)"等...
我真的很困惑,无法在任何地方找到答案。
答案 0 :(得分:0)
如果您尝试覆盖当前sheet1中的内容:
Sub copySheet()
Dim srcBook As Workbook
Set srcBook = Application.Workbooks.Open(ThisWorkbook.Path & "\Book1.xlsx")
srcBook.Sheets("Sheet1").UsedRange.Copy Destination:=ThisWorkbook.Sheets("Sheet1").Cells(1,1)
srcBook.Close False
End Sub
答案 1 :(得分:0)
这里有两个可能的任务:
听起来你真正想做的是将Range复制到现有的工作表。试试这个:
Sub copySheetContents()
Dim srcBook As Workbook
Set srcBook = Application.Workbooks.Open(ThisWorkbook.Path & "\Book1.xlsx")
srcBook.Sheets("Sheet1").UsedRange.Copy
ThisWorkbook.Sheets(1).Cells(1, 1).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
srcBook.Close False
End Sub
请注意,上面假设您在源工作簿中使用的范围从单元格A1开始。