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, errors are not intentional so that makes the finding the cause for the error makes 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.
For an 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 VBA data type we are going to use. For an example look at the below code.
Sub OverFlowError_Example1() Dim Number As Byte Number = 256 MsgBox Number End Sub
For the variable “Number” I have assigned the value as 256. When I run this code we will get the below error.
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
The integer is the data type in VBA which can hold values from -32768 to 32767. For an example look at the below code.
Sub OverFlowError_Example2() Dim MyValue As Integer MyValue = 25656 MsgBox MyValue End Sub
When I run this code we will get the value of the variable “MyValue” in the message box i.e. 25656.
Now I will reassign the number to the variable as “45654”.
Sub OverFlowError_Example2() Dim MyValue As Integer MyValue = 45654 MsgBox MyValue End Sub
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.
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.
Sub OverFlowError_Example3() Dim MyValue As Long MyValue = 5000 * 457 MsgBox MyValue End Sub
This will cause an overflow error.
To fix this issue we need to use the function CLNG in VBA. Below is an example of the same.
Sub OverFlowError_Example3() Dim MyValue As Long MyValue = CLng (5000) * 457 MsgBox MyValue End Sub
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
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 –