VBA OverFlow Error

Last Updated :

-

Edited by :

Reviewed by :

Table Of Contents

arrow

Excel VBA OverFlow Error

Errors are part and parcel of any coding language but finding why that error is coming is what makes you stand apart from the crowd in interviews. Errors are not strange to VBA coding. However, errors are not intentional, so finding the cause for the error is a hard task. In VBA, we have some predefined errors, and knowing about them makes you quickly fix the bug. This article will show you the RUN TIME ERROR 6: Overflow. Follow the full article to learn about the error, the reasons for the VBA “Overflow error,” and how to fix them.

What is Run Time Error 6: Overflow Error in VBA?

When we declare the variable, we assign a data type to them. We should be completely aware of each data type's pros and cons—this is where "Run Time Error 6: Overflow" comes into the picture. When we overload the data type with a value, which is more than the capacity of the data type, then we will get this error.

VBA OverFlow Error

For example: If you declare the variable as Byte.

Dim Number As Byte

The Byte data type can hold values from 0 to 255. Now, we will assign the value to 240.

Number = 240

It should work fine because the value we have assigned is less than the limit of Byte's value of 255. However, the moment we assign the value, which is more than 255, it leads to the error of Run Time Error 6: Overflow.

It is the general overview of the Run Time Error 6: Overflow. Next, we will see some of the examples in detail.

Examples of Run Time Error 6: OverFlow in VBA

Let us see some examples of VBA overflow errors in Excel.

Example 1: OverFlow Error with Byte Data Type

Knowing the pros and cons of the VBA data type we will use is important. For example, look at the below code.

Code:

Sub OverFlowError_Example1()

Dim Number As Byte

Number = 256

MsgBox Number

End Sub
overflow error example 1.1

For the variable "Number,"we have assigned the value as 256. Therefore, we will get the below error when we run this code.

overflow error example 1.2

The data type Byte can hold values from 0 to 255. So it causes an error. To fix the error, we either change the data type or reduce the value assigned to the variable "Number."

Example 2: VBA OverFlow Error with Integer Data Type

VBA integer is a data type that can hold values from -32768 to 32767. For example, look at the below code.

Code:

Sub OverFlowError_Example2()

Dim MyValue As Integer

MyValue = 25656

MsgBox MyValue

End Sub
overflow error example 2.1

When we run this code, we will get the variable "MyValue" value in the message box, i.e., 25656.

overflow error example 1.4

Now, we will reassign the number to the variable as "45654."

Code:

Sub OverFlowError_Example2()

Dim MyValue As Integer

MyValue = 45654

MsgBox MyValue

End Sub
overflow error example 2.2

Now, if I try to run the code, it will cause an error because the data type we have declared can only hold the maximum of 32767 for positive numbers, and for negative numbers, the limit is -32768.

overflow error example 1.2

Example 3: VBA OverFlow Error with Long Data Type

The Long data type is the most often used in Excel VBA. This can hold values from –2,147,483,648 to 2,147,486,647. Anything above that will cause an error.

Code:

Sub OverFlowError_Example3()

Dim MyValue As Long

MyValue = 5000 * 457

MsgBox MyValue

End Sub
run time error 6: example 3.1

It will cause an overflow error.

run time error 6: example 3.2

We need to use the function CLNG in VBA to fix this issue. Below is an example of the same.

Code:

Sub OverFlowError_Example3()

Dim MyValue As Long

MyValue = CLng (5000) * 457

MsgBox MyValue

End Sub
run time error 6: example 3.3

It should work fine.

It is the overview of the Run Time Error 6: Overflow.We must be completely aware of the data types to solve this error. So go back to basics, do the basics right, then everything will fall in place.

You can download this VBA Overflow Error Excel Template here - VBA OverFlow Error Excel Template