XL2000: Error Using Macro to Open or Activate a Workbook (271595)
The information in this article applies to:
This article was previously published under Q271595 SYMPTOMS
When you attempt to activate a workbook with a Visual Basic for Applications procedure, you may receive one of the following error messages:
Run-time error '9':
Subscript out of range
Run-time error '1004':
Workbook method of Application class failed
CAUSE
This problem can occur if either of the following conditions is true:
- You attempt to activate a workbook that is not open.
-or-
- You attempt to activate a workbook without properly qualifying the workbook name.
WORKAROUND
To work around this issue, you can also use the following steps to activate the workbook. This example opens the workbook regardless of how you reference the workbook to be activated.
- Open a new workbook in Microsoft Excel, and then start the Visual Basic Editor (press ALT+F11).
- On the Insert menu, click Module.
- In the module sheet, type the following code:
Sub ActivateSheet()
On Error Resume Next
' Try to activate Book2 in a unsaved state.
Workbooks("Book2").Activate
' Try to activate Book2.xls in a saved state.
Workbooks("Book2.xls").Activate
' End Error checking.
On Error GoTo 0
' Check to see if Book2 is opened, if not, open it.
If Not ActiveWorkbook.Name = "Book2.xls" And Not _
ActiveWorkbook.Name = "Book2" Then
Workbooks.Open Filename:="Book2.xls"
End If
End Sub
NOTE: If Book2 does not exist, you receive the following error message:
Run-time Error '1004':
'Book2.xls' could not be found.
Modification Type: | Minor | Last Reviewed: | 1/6/2006 |
---|
Keywords: | kbpending kbprb KB271595 |
---|
|