Microsoft KB Archive/157411

{|
 * width="100%"|

XL97: Error Message for Sheets.Select Has Changed

 * }

Q157411

-

The information in this article applies to:


 * Microsoft Excel 97 for Windows

-

SYMPTOMS
When you run a macro in Microsoft Excel 97 for Windows 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

This error message is different from the error message that you receive in earlier versions of Microsoft Excel.

RESOLUTION
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 see the following page on the World Wide Web:

"http://www.microsoft.com/partner/referral/" For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

"http://support.microsoft.com/directory/overview.asp" In earlier versions of Microsoft Excel, when a 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.

Because the error code has changed, when you write an error handling routine that specifically tests for this run-time error, make sure that your routines reflect the new error code, and not the error code from previous versions.

The following sample 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 preceding macro in a workbook that does not have a sheet called "nonexistent," a message box with the following message appears:

encountered an error