VBA OverFlow Error
Last Updated :
-
Blog Author :
Edited by :
Reviewed by :
Table Of Contents
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.
Table of contents
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.
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
For the variable "Number,"we have assigned the value as 256. Therefore, we will get the below error when we run this code.
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
When we run this code, we will get the variable "MyValue" value in the message box, i.e., 25656.
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
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.
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
It will cause an overflow error.
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
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
Recommended Articles
This article has been a guide to VBA Overflow Error. Here, we learn how Runtime 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: -