I am importing a worksheet from another workbook to my current workbook. After I complete importing the worksheet, I want to close that other workbook. The code I am using gives the error Run-time error 9': Subscript out of range.
Sub ImportWorksheet(MyPath As String, wbName As String)
ControlFile = ActiveWorkbook.Name
Workbooks.Open Filename:=MyPath
Sheets(1).Copy After:=Workbooks(ControlFile).Sheets(1)
ActiveSheet.Name = wbName
Workbooks(MyPath).Close SaveChanges:=False
Windows(ControlFile).Activate
End Sub
I also tried using
Windows(MyPath).Activate
ActiveWorkbook.Close SaveChanges:=False
But I get the same error.
MyPathis a file path, which works fine when you're opening a workbook by it'sFilename, but when usingWorkbooks().Closeyou need to list the workbook's name, not it's path. Try usingWorkbooks(Dir(MyPath)).Close SaveChanges:=False.