XL97: Error Message for Sheets.Select Has Changed

Last reviewed: March 13, 1998
Article ID: Q157411
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

In Microsoft Excel 97 for Windows, when you run a macro that attempts to select a worksheet that does not exist in a workbook, you may receive the following error message :

   Run-time error '9':
   Subscript out of range

NOTE: A different error message appears in earlier versions of Microsoft Excel.

MORE INFORMATION

In earlier versions of Microsoft Excel, when the macro attempts to select a sheet that does not exist in a workbook, you receive the following error message:

   Run-time error '1004':
   Sheets method of Application class failed

The new error message reflects the fact that the Sheets Collection object in Microsoft Excel 97 returns an array of all sheets in a workbook. The error message "Subscript out of range," indicates that the macro references a nonexistent array element or collection member. The subscript may be larger or smaller than the range of possible subscripts, or the array may not yet have assigned dimensions in the application.

NOTE: When you write an error handling routine that specifically tests for the run-time error, you must change the routines to reflect the new error code.

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 engineers 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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

The following example demonstrates how to use an error handling routine, and a routine that tests whether a worksheet exists in Microsoft Excel 97:

   Sub Sample_Error()

      On Error GoTo errorhandler   'Starts Error Trapping

      Sheets("nonexistent").Select 'Attempts to select sheet called
                                   'nonexistent

      Exit Sub                     'Skips the error handling routine

   errorhandler:
      If Err.Number = 9 Then       'This line is different from earlier
                                   'versions.

         MsgBox "encountered an error"

      End If

   End Sub

If you run the above macro in a workbook that does not have a sheet called nonexistent, a message box with the following message appears:

   encountered an error

REFERENCES

For more information about the Sheets Collection object, click the Index tab in Microsoft Visual Basic Help, type the following text

   sheets

and then double-click the selected text to go to the "Sheets Collection Object" topic.


Additional query words: XL97
Keywords : kbcode kberrmsg kbprg xlvbainfo
Version : WINDOWS:97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.