DavidH7470
asked on
VB close an open workbook from the macro of another workbook
I have an excel workbook that runs a macro when it opens that will open another workbook in read only and then close the workbook that the macro openned. I do this from other macro's quite often and simply have the macro select the workbook that it openned and then do a file close. But with this particular macro I get the error. "An Error has occurred: 9 - Subscript out of range" and the line Windows("Workbook2.xls").A ctivate
is highlighted
The code is:
(Run from Workbook1 when is opens)
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks.Open Filename:="C:\Workbook2.xl s", UpdateLinks:=0, ReadOnly:=True
Windows("Workbook2.xls").A ctivate
ActiveWindow.Close
End Sub
is highlighted
The code is:
(Run from Workbook1 when is opens)
Private Sub Workbook_Open()
Application.ScreenUpdating
Application.DisplayAlerts = False
Workbooks.Open Filename:="C:\Workbook2.xl
Windows("Workbook2.xls").A
ActiveWindow.Close
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks it worked great
Rather than use Windows, why not Workbooks? Its a better approach, as well.
Try:
Open in new window
Dave