XL2000: Error Using Macro to Open or Activate a Workbook (271595)



The information in this article applies to:

  • Microsoft Excel 2000

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

- or -

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.
  1. Open a new workbook in Microsoft Excel, and then start the Visual Basic Editor (press ALT+F11).
  2. On the Insert menu, click Module.
  3. 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.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
To better understand when this problem occurs with the .Activate method and how to avoid the problem, please see the following chart.
Workbook Saved       Workbook Open      Referenced With     Result
--------------       -------------      ---------------     ------
Yes                  Yes                Book2.xls           No Error
Yes                  No                 Book2.xls           Error
Yes                  No                 Book2               Error 
No                   Yes                Book2.xls           Error    
No                   Yes                Book2               No Error
				
NOTE: Considering the chart above, there are two main points to remember when activating a workbook. If the workbook being referenced has been saved, use the .xls extension. If the workbook being referenced has not been saved, do not use the .xls extension.

In addition, it is important to remember that the Activate Method is not intended to open a workbook. Considering this, you should open the workbook you will be working with before you try to activate it.

Modification Type:MinorLast Reviewed:1/6/2006
Keywords:kbpending kbprb KB271595