Runtime error 6 ("Overflow") appears when VBA tries to store a number in a variable type that is too small to hold it — for example, putting 40,000 into an Integer, which tops out at 32,767. The fix is to find the line the debugger highlights, change the variable's declared type to a wider one (usually Long or Double), and convert any Excel cell, TextBox or InputBox value into a real number before assigning it. The steps below walk through diagnosing it, fixing it, and preventing it from recurring.
What You'll Need
- Excel with a workbook containing VBA code (saved as .xlsm or .xlsb if you want to keep macros)
- Access to the Visual Basic Editor (Alt+F11 on Windows; on Mac, Developer tab > Visual Basic)
- A recent backup of the workbook, since code changes are easiest to undo with a saved copy
Step-by-Step
Reproduce the error with the debugger attached. Run the macro the same way you normally trigger it. When the error dialog appears, click Debug rather than End. The Visual Basic Editor opens with one line highlighted in yellow. That highlighted line is where the overflow was detected, which is usually the line doing the assignment, not the line where the too-large value was created.
Read the highlighted line and the variable on its left. Typical patterns look like
myCount = Range("A1").Value,total = total + amount, orrowNum = someValue * 1000. Note whether the variable on the left is declared asInteger,Byte, or as an untypedVariantthat is really holding a number. Label menu and dialog wording can differ slightly between Excel versions and platforms, but the Editor's behaviour is the same.Check the actual value at runtime. With the code paused, hover the mouse over the variable on the right side of the highlighted line, or open the Immediate window (Ctrl+G) and type
?variableNamethen press Enter. In the example, ifRange("A1").Valueholds 45,000 while the target is an Integer, the overflow is confirmed. If nothing shows, add a temporaryDebug.Printline above the failing line, run again, and read the Immediate window.Widen the variable's data type. Go to the declaration (near the top of the procedure or the module's declarations section) and change it. Use these replacements:
Integer→Long(covers roughly ±2.1 billion instead of ±32,767). Row numbers and worksheet counts should almost always beLong.Long→LongLongon 64-bit Office, orDoubleif decimals may be involved.Single→Doublefor money or large fractional values.Currencyis a good choice for cash amounts up to about 922 trillion with four decimal places.
If the variable was never declared, add
Option Explicitat the top of the module so VBA forces every variable to be declared, then declare the widened types explicitly.Convert values that arrive from outside VBA. Cells, TextBoxes, InputBox results and parsed CSV fields are text or Variants until you convert them. Use
CLng,CDbl,CCurorCDecto convert, and wrap inIsNumeric:If IsNumeric(Range("A1").Value) Then myCount = CLng(Range("A1").Value). Note that in VBA,IntandCLngare not interchangeable in behaviour at the extremes —CLngis what you want for integer conversion, whileCDblhandles decimals.Look for intermediate calculations that overflow before assignment. The failing line may be a multiplication or addition where both operands are small but the result is huge, or where a subtraction loops below zero into an unsigned or Byte variable. Split the expression into several statements and convert the widest intermediate first, e.g.
Dim big As Double: big = CDbl(a) * CDbl(b).Check loop counters and array boundaries.
For i = 1 To UBound(arr)is safe, butFor i = 1 To 40000withDim i As Integeroverflows silently at 32,768 and leaves the loop in a strange state. Declare all loop counters asLong. The same applies to worksheet row variables: Excel has far more rows than an Integer can hold.Recompile and rerun. In the Visual Basic Editor, choose Debug > Compile VBAProject to catch syntax and type problems before running. Then run the macro again from Excel, or press F5 in the Editor. If the previous error was on a line assigning to a Variant, check that the Variant still holds the intended number type after your edits.
Save and test with edge cases. Test with zero, a negative number, a value just above 32,767, a large decimal, and an empty cell. Then save the workbook. If the error previously occurred only on some data, copy a few of those rows into a test sheet and run the macro against them before deploying the workbook.
Common Mistakes
- Changing only the line that errors. The variable that holds the oversized value may be declared earlier in the procedure; change that declaration, not just the assignment.
- Assuming Long is unlimited. A Long still maxes out around 2.1 billion. Summing many large numbers, or multiplying two moderate ones, can exceed it — use
DoubleorCurrencyfor those. - Using Integer out of habit. Many older code samples and tutorials use
Integer. In modern VBA,Longis the standard choice because it is no slower on current hardware and avoids the most common overflow. - Trusting InputBox or TextBox text. An InputBox returns a String.
myNum = InputBox("Enter a number")followed by arithmetic can fail or misbehave if the user types spaces, commas or blank. Convert withCLngorCDblafter validating. - Ignoring negative underflow. Assigning a negative result to a
Byte, or subtracting past the lower bound of an Integer, also triggers error 6. Watch subtraction in loops.
Tips
- Put
Option Explicitat the top of every module. It turns silent Variant mistakes into immediate, visible compile errors. - For money, prefer
CurrencyorDecimal(viaCDec) overDoubleto avoid rounding drift in totals. - Use
On Error GoTowith a handler that printsErr.DescriptionandErl(with line numbers) to the Immediate window so future errors are easier to trace. - Use breakpoints (F9) and step through with F8 around loops that process whole columns, rather than running the entire macro repeatedly.
- Keep a copy of the working macro in a separate module or text file before rewriting declarations, so you can compare and roll back.
Troubleshooting
The error returns on the same line after widening the variable. Something upstream is overflowing before it reaches that line, or the value is not a number at all. Check with ?TypeName(variable) and ?variable in the Immediate window at a breakpoint.
The highlighted line is a worksheet operation. Row or column variables declared as Integer can overflow when a range reference is built, such as Cells(i, j). Declare those variables as Long.
Compile errors appear after your edits. A type may have been mistyped or duplicated. Run Debug > Compile VBAProject again, fix each compile error in turn, and check the declarations section carefully.
The error only happens on other people's machines. Another copy of the workbook may contain old code, or the other machine may be 32-bit Office with different limits. Confirm everyone is running the current saved version and check LongLong compatibility with the installed Office bitness.
You cannot find the offending variable. Use Find in the Editor (Ctrl+F) to search for As Integer and As Byte across the whole project. Replace them methodically. If the workbook is locked or the VBA project is password-protected, you will need the password or a clean copy from whoever maintains it; there is no legitimate way around it.
Frequently Asked Questions
What causes runtime error 6 overflow in Excel VBA?
A value is too large (or too small) for the data type of the variable receiving it. The most common case is an Integer receiving a value above 32,767, but Byte, Long and even Single variables can overflow.
Why use Long instead of Integer?
Long holds values up to about 2.1 billion, which safely covers Excel row counts and most counters. Integer's limit of 32,767 is easily exceeded by real worksheet data.
Does error 6 mean my macro is corrupt?
No. It is a normal runtime error indicating a type mismatch of size. Fixing the declared type or converting the input value resolves it.
Can I catch error 6 without changing the code?
You can trap it with On Error GoTo, but the value still will not fit. Trapping only hides the symptom; widening the variable is the real fix.
Why does the error appear only with certain rows or files?
Because those rows contain numbers large enough to exceed the variable's limit. A test with small sample data may pass while larger real data fails.
Does upgrading Office fix it?
Not by itself, though 64-bit Office enables LongLong for extreme integer values. The declaration in your code is what determines whether an overflow occurs.