XL97: Indexes for Methods and Collections Are Rounded (159874)



The information in this article applies to:

  • Microsoft Excel 97 for Windows

This article was previously published under Q159874

SYMPTOMS

In Microsoft Excel 97, if you specify a decimal value for the index of a method or collection in a Visual Basic for Applications macro, the index is rounded to the nearest whole number. However, in earlier versions of Microsoft Excel, the index is truncated rather than rounded. Because of this difference, you may receive different results from different versions of Microsoft Excel.

CAUSE

Microsoft Excel 97 uses strict typing for arguments of collections and methods. If an argument is typed as an integer, decimal values that are passed to that argument are automatically rounded.

This behavior is by design 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 visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: You should not rely on Microsoft Excel to automatically convert your decimal values for indexes. If you require that the index is to be truncated in all versions of Microsoft Excel, use the INT function:
   MsgBox Sheets(INT(1.6)).Name
				
If you require that the index is to be rounded in all versions of Microsoft Excel, use the CInt function:
   MsgBox Sheets(CInt(1.6)).Name
				

MORE INFORMATION

The following sample demonstrates a situation where you receive conflicting results in different versions of Microsoft Excel.
   MsgBox Sheets(1.6).Name
				
In Microsoft Excel 97, 1.6 is rounded to 2; and, this line of code will reference the second Sheet in the Sheets collection. In Microsoft Excel versions 5.0 and 7.0, the number 1.6 is truncated to 1; and, this line of code references the first Sheet in the Sheets collection.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbdtacode kbprb kbProgramming KB159874