Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Error Using Macro to Open or Activate a Workbook


View products that this article applies to.

This article was previously published under Q271595

↑ Back to the top


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

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


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 Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
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.

↑ Back to the top


Keywords: KB271595, kbprb, kbpending

↑ Back to the top

Article Info
Article ID : 271595
Revision : 6
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 235