将工作表复制到另一个工作簿时运行时错误“9”

时间:2013-07-17 13:55:05

标签: excel excel-vba runtime-error worksheet vba

我一直在尝试编写一个宏来将“Sheet1”从一个工作簿复制到另一个工作簿,但我不断收到运行时错误“9”:下标超出范围。

Sub CopySheetToOtherWbk()

Dim CopyFromBook As Workbook
Dim CopyToWbk As Workbook
Dim ShToCopy As Worksheet

Set CopyFromBook = Workbooks("AllBugs.xlsx")
Set ShToCopy = CopyFromBook.Worksheets("Sheet1")
Set CopyToWbk = Workbooks("YourFriendlyNeighborhoodTemplateWorksheet.xlsx")

ShToCopy.Copy After:=CopyToWbk.Sheets(CopyToWbk.Sheets.Count)

End Sub

突出显示的行是“Set CopyFromBook = Workbooks(”AllBugs.xlsx“)”。不知道我在这里做错了什么。相对较新的VBA。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

Workbooks集合指的是所有当前打开的工作簿。如果工作簿未打开,则需要先执行此操作。

Set CopyFromBook = Workbooks.Open("C:\Some Location\AllBugs.xlsx")