Question

I've been trying to write a macro to copy "Sheet1" from one workbook into another, but I keep getting a run-time error '9': Subscript out of range.

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

The highlighted line is "Set CopyFromBook = Workbooks("AllBugs.xlsx")". Not sure what I'm doing wrong here. Relatively new to VBA. Any help is appreciated.

Was it helpful?

Solution

The Workbooks collection refers to all currently open workbooks. If the workbook isn't open you will need to do this first.

Set CopyFromBook = Workbooks.Open("C:\Some Location\AllBugs.xlsx")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top