VBA OverFlow Error

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 codingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more. Errors are not intentional, so that makes finding the cause for the error makes the hard task. In VBA, we have some of the pre-defined errors, and knowing about them makes you fix the bug very quickly. In this article, we will show you about the RUN TIME ERROR 6: OverFlow. Follow the full article to know about the error, 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 the pros and cons of each data type—this where Run Time Error 6: OverFlow comes into the picture. When we overload the data type with the value, which is more than the capacity of the data type, then we will get this error.

VBA OverFlow Error

You are free to use this image on your website, templates etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA OverFlow Error (wallstreetmojo.com)

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 I will assign the value as 240.

Number = 240

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

This is the general overview of the Run Time Error 6: OverFlow. We will see some of the examples in detail.

Examples of Run Time Error 6: OverFlow in VBA

Let’s see some examples of VBA OverFlow Error in Excel.

Example 1: OverFlow Error with Byte Data Type

As I told, it is important to know the pros and cons of the VBA data type we are going to use. 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,” I have assigned the value as 256. When I run this code, we will get the below error.

overflow error example 1.2

This is because the data type Byte can hold values from 0 to 255. So it causes an error. To fix the error, either we have to change the data type, or we have to reduce the value we have 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 I run this code, we will get the value of the variable “MyValue” in the message box, i.e., 25656.

overflow error example 1.4

Now I 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 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 data type 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

This will cause an overflow error.

run time error 6: example 3.2

To fix this issue, we need to use the function CLNG in VBACLNG In VBAVBA CLng or "VBA Convert to Long" is an in-built Excel function that facilitates converting the numerical values or data exceeding the long data type limit into the acceptable data type.read more. 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

This should work fine.

This is the overview of the Run Time Error 6: OverFlow. To solve this error, we need to completely aware of the data types. 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

Recommended Articles

This has been a guide to VBA OverFlow Error. Here we learn how Run Time Overflow Error 6 occurs in Excel VBA and how to handle this error along with practical examples and a downloadable template. Below are some useful excel articles related to VBA –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>