VBA Runtime Error 6

Written by

Mel Jenkins

Reviewed by

Steve Rynearson

Last updated on May 2, 2022

This article will explain the VBA Runtime error 6.

vba error overflow

Run-time error 6 in VBA is the Overflow error. This means that a variable has been declared as one numeric data type, and then populated with a number that is outside the parameters of that data type.

Consider the following code:

Sub OverflowError()
 Dim i As Integer
 i = 600000
End Sub

We are declaring i as an Integer variable. An integer variable can hold the values of ‑32,768 to 32,768.  We are trying to assign the value of 600,000, which is outside the allowed range and therefore the error will occur.

If we click on Debug when this error occurs, the error line will appear in yellow.

vba error overflow debug

To solve this error, make sure you declare the variable with the correct numeric data types to hold the required data:

vba error datatypes

In this case use the Long Variable type.

Sub OverflowError_Corrected()
 Dim i As Long
 i = 600000
End Sub

We recommend always using the Long variable type instead of the Integer variable type. The only advantage of the Integer variable type is less memory is required. However, this is mostly irrelevant for coding running from VBA on modern computers.

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples