XL97: Error Message for Sheets.Select Has ChangedLast reviewed: March 13, 1998Article ID: Q157411 |
The information in this article applies to:
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 rangeNOTE: A different error message appears in earlier versions of Microsoft Excel.
MORE INFORMATIONIn 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 failedThe 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.aspThe 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 SubIf 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 REFERENCESFor more information about the Sheets Collection object, click the Index tab in Microsoft Visual Basic Help, type the following text
sheetsand then double-click the selected text to go to the "Sheets Collection Object" topic.
|
Additional query words: XL97
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |