Microsoft KB Archive/271595

= XL2000: Error Using Macro to Open or Activate a Workbook =

Article ID: 271595

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



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.  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(&quot;Book2&quot;).Activate

' Try to activate Book2.xls in a saved state. Workbooks(&quot;Book2.xls&quot;).Activate

' End Error checking. On Error GoTo 0

' Check to see if Book2 is opened, if not, open it. If Not ActiveWorkbook.Name = &quot;Book2.xls&quot; And Not _ ActiveWorkbook.Name = &quot;Book2&quot; Then Workbooks.Open Filename:=&quot;Book2.xls&quot; 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:

https://partner.microsoft.com/global/30000104

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.

Additional query words: XL2000

Keywords: kbpending kbprb KB271595

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.