0

我一直在尝试编写一个宏来将“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 相对较新。任何帮助表示赞赏。

4

1 回答 1

1

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

Set CopyFromBook = Workbooks.Open("C:\Some Location\AllBugs.xlsx")
于 2013-07-17T13:59:57.443 回答