WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA OverFlow Error

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 coding. 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

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.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

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 VBA. 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 –

  • VBA Pivot Table
  • Clear Contents in VBA
  • Excel VBA On Error Goto 0
  • How to Delete Files using VBA Code?
0 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

New Year Offer - VBA Training Course (6 courses, 35+ hours video) View More