"Overflow" Error Running Procedure with Math Calculation (125900)



The information in this article applies to:

  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95
  • Microsoft Excel for Windows 5.0
  • Microsoft Excel 98 Macintosh Edition

This article was previously published under Q125900

SYMPTOMS

In 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

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, 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 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, 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 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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to 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 requirements. 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, you can use either of the following methods:

Method 1

Use 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 2

Use 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
				

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products 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 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.

REFERENCES

For a Data Type Summary, choose the Search button in the Visual Basic Reference and type:

data types


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbbug kbdtacode kbProgramming KB125900