XL: "Overflow" Error Running Procedure with Math CalculationLast reviewed: February 3, 1998Article ID: Q125900 |
The information in this article applies to:
SYMPTOMSIn Microsoft Excel, when you run a Microsoft Visual Basic for Applications procedure that contains a mathematical calculation, you may receive the following error message:
Run-time error '6': Overflow CAUSEThis 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, you receive this error message when you run the following procedure:
Sub Test() Dim MyVarInteger As Integer Dim MyVarDouble As Double MyVarInteger = 256 MyVarDouble = 256 * MyVarInteger End SubThe 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, 65536, is larger than the range for an Integer data type (which must be between -32768 and 32767). 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. You also receive this error message when you run the following procedure:
Sub Test2() x = (3832908 * 1000) / (2 * 218706) MsgBox x End SubIn 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.
WORKAROUNDMicrosoft 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.aspTo 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, you can use either of the following methods:
Method 1Use a data type conversion function, such as CLng in the following example:
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 2Use a type-declaration character as in the following examples:
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 Sub Test2() ' Follow 1000 with number sign to identify value as Double data type. x = (3832908 * 1000#) / (2 * 218706) MsgBox x End Sub STATUSMicrosoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.
MORE INFORMATIONThe Help topic for this error message indicates that a possible cause for this error message includes 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.
REFERENCESFor a Data Type Summary, choose the Search button in the Visual Basic Reference and type:
data types |
Additional query words: 5.00 5.00a 5.00c 7.00 7.00a XL98 XL97 XL7 XL5
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |