Microsoft KB Archive/213825

From BetaArchive Wiki

Article ID: 213825

Article Last Modified on 11/23/2006



APPLIES TO

  • Microsoft Excel 2000 Standard Edition



This article was previously published under Q213825


SYMPTOMS

In Microsoft Excel, when you run a Microsoft Visual Basic for Applications procedure that contains a mathematical calculation, the following error message may appear:

Run-time error '6':
Overflow

CAUSE

This error message appears when the mathematical calculation involves numbers or variables of one data type, such as Integer, and you assign the result of the calculation to a variable of a different data type, such as Double or Long, even if the result of the calculation is within the range of the data type for the resulting variable. For example, the error message appears when you run the following procedure:

Sub Test()
    Dim MyVarInteger As Integer
    Dim MyVarDouble As Double
    MyVarInteger = 256
    MyVarDouble = 256 * MyVarInteger
End Sub
                

The error message occurs in this case because the number 256 is a constant of Integer data type. Because the variable MyVarInteger is also a value of Integer data type, the multiplication calculation is performed as an Integer calculation. The error message occurs because the result of the calculation, 65,536, is larger than the range for an Integer data type (which must be between -32,768 and 32,767).

By declaring the result, MyVarDouble, as Double data type, the calculation multiplies the two Integer data types and then attempts to convert the result to a Double data type. Because the result is not within the range for an Integer data type, the error occurs before the result is converted to the Double data type.

The error message also appears when you run the following procedure:

Sub Test2()
    x = (3832908 * 1000) / (2 * 218706)
    MsgBox x
End Sub
                

In this example, because the values in the calculation are constants, you cannot dimension the resulting variable, x, as Double, because you cannot convert an Integer to a Double data type internally by assigning the result of a calculation that contains an Integer to a Double data type.

WORKAROUND

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:


To avoid this error message when you perform a mathematical calculation in a Visual Basic procedure, you must convert at least one of the operands to a data type with a range that is greater than the resulting value. This forces the calculation to be performed using the largest data type. To do this, use either of the following methods.

Method 1

Use a data type conversion function, such as CLng in the following sample:

Sub Test()
   Dim MyVarInteger As Integer
   Dim MyVarDouble As Double
   MyVarInteger = 256
   ' Use CLng function to convert MyVarInteger to Long data type.
   MyVarDouble = 256 * CLng(MyVarInteger)
End Sub
                

Method 2

Use a type-declaration character, as in the following samples:

Sample 1

Sub Test()
   Dim MyVarInteger As Integer
   Dim MyVarDouble As Double
   MyVarInteger = 256
   ' Follow 256 with an ampersand to identify value as Long data type.
   MyVarDouble = 256& * MyVarInteger
End Sub
                

Sample 2

Sub Test2()
   ' Follow 1000 with number sign to identify value as Double data type.
   x = (3832908 * 1000#) / (2 * 218706)
   MsgBox x
End Sub
                

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

The Help topic for this error message indicates that a possible cause for this error message is the following:

The result of an assignment, calculation, or data type conversion is too large to be represented within the range allowed for that type of variable.


For example, the range allowed for a variable of data type Double is -1.79769313486232E308 to -4.94065645841247E-324 (for negative values) and 4.94065645841247E-324 to 1.79769313486232E308 (for positive values). The range allowed for a variable for data type Long is -2,147,483,648 to 2,147,483,647.

REFERENCES

For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 OFF2000: How to Run Sample Code from Knowledge Base Articles



For more information about data types, click Microsoft Visual Basic Help on the Help menu, type using data types efficiently in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.


Keywords: kbbug kbdtacode kberrmsg kbpending kbprogramming KB213825