Excel 2010 báo lỗi run time error 6

Run-time error 6 in VBA is the Overflow error. This means that a variable has been declared as one numeric data type, and then populated with a number that is outside the parameters of that data type.

Consider the following code:

Sub OverflowError()  
 Dim i As Integer  
 i = 600000  
End Sub

We are declaring i as an Integer variable. An integer variable can hold the values of ‑32,768 to 32,768. We are trying to assign the value of 600,000, which is outside the allowed range and therefore the error will occur.

If we click on Debug when this error occurs, the error line will appear in yellow.

Excel 2010 báo lỗi run time error 6

To solve this error, make sure you declare the variable with the correct numeric data types to hold the required data:

Excel 2010 báo lỗi run time error 6

In this case use the Long Variable type.

Sub OverflowError_Corrected()  
 Dim i As Long  
 i = 600000  
End Sub

We recommend always using the Long variable type instead of the Integer variable type. The only advantage of the Integer variable type is less memory is required. However, this is mostly irrelevant for coding running from VBA on modern computers.

I am a Programmer but never use VBA. I am taking over someone's old script and it keeps failing at a certain point with a Run-Time Error.

Excel 2010 báo lỗi run time error 6

The Debugging report is here:

Excel 2010 báo lỗi run time error 6

Please let me know if you need more code. I have tried to change currRow to Long, single, double, string none work. Below is a quick glimpse of the sheet that is failing. The top row is row one.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Overflow (Error 6)

  • Article
  • 09/13/2021

In this article

An overflow results when you try to make an assignment that exceeds the limitations of the target of the assignment. This error has the following causes and solutions:

  • The result of an assignment, calculation, or data type conversion is too large to be represented within the range of values allowed for that type of variable. Assign the value to a variable of a type that can hold a larger range of values.
  • An assignment to a property exceeds the maximum value the property can accept. Make sure your assignment fits the range for the property to which it is made.
  • You attempt to use a number in a calculation, and that number is coerced into an integer, but the result is larger than an integer. For example: Dim x As Long
      x = 2000 * 365   ' Error: Overflow  
    

    To work around this situation, type the number, like this:

    Dim x As Long
      x = CLng(2000) * 365  
    

For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.

In VBA, Overflow (Error 6) is a run-time error that occurs when you specify a number to the variable that is out of the range of numbers which that data type can take. In simple words, this error occurs when you go out of the range for a variable’s type.

Let’s say you are using the Integer data type that can take values ranging from -32,768 to 32,767 so when you specify a value that is out of this range you get the Overflow run time error.

Excel 2010 báo lỗi run time error 6

In the above example, you can see that we have used the integer data type for the iNum variable but while specifying the value we have used “10000000” which is way more than the range, and when you run the code Overflow run-time error occurs.

Excel 2010 báo lỗi run time error 6
Sub myMacro()
Dim iNum As Integer
iNum = 10000000
End Sub

How to Deal with Overflow (VBA Error 6)

The way to deal with this error is to have a complete understanding of the VBA Data Types that you need to use while declaring a variable. You need to deal with a range of values when you are using a data type to store a numeric value in the variable. So, you need to examine the range of the result that you want to store in the variable.

Understanding of data types and their range of values to store can help you correct this error. It can also help you to not to write a code in way which runs in the Overflow error.