VBA raises Runtime Error 6: Overflow when a calculation, assignment, or counter produces a number that exceeds the physical memory allocation and storage limits of the target variable’s data type. In Excel, this most frequently occurs when a row counter or loop variable is declared as an Integer and reaches row 32,768.
Quick Answer
To eliminate Runtime Error 6 in loop counters and row references, replace all
Integervariable declarations withLong. The legacyIntegertype caps out at 32,767, while modern Excel sheets contain 1,048,576 rows. ALonginteger safely accommodates up to 2,147,483,647.
The 16-Bit Integer Ceiling
In older versions of VBA dating back to 16-bit operating systems, an Integer was allocated 2 bytes (16 bits) of memory. This restricts its range to values from -32,768 to 32,767.
A modern Excel worksheet has 1,048,576 rows. If your macro loops past row 32,767 or counts rows in a table exceeding that number, an Integer overflows:
Sub CauseOverflow()
Dim r As Integer ' Max capacity: 32,767
' Crashes on iteration 32,768 with Runtime Error 6:
For r = 1 To 50000
Cells(r, 1).Value = r
Next r
End Sub
Even if you never expect 30,000 rows, reading the last used row in a column using .End(xlUp).Row will immediately overflow if the column is blank or the search hits the bottom of the worksheet:
Dim lastRow As Integer
' If column A is empty, End(xlUp) looks at row 1,048,576, crashing immediately:
lastRow = Cells(Rows.Count, "A").End(xlUp).Row ' Error 6
VBA Numeric Data Type Limits
To prevent overflow errors across different calculations, match your data to the appropriate type:
| Data Type | Memory Size | Value Range | Common Use Case |
|---|---|---|---|
| Byte | 1 byte | 0 to 255 | Character codes, small flags |
| Integer | 2 bytes | -32,768 to 32,767 | Legacy code only (avoid in modern Excel) |
| Long | 4 bytes | -2,147,483,648 to 2,147,483,647 | All worksheet row/column indexing |
| LongLong (64-bit) | 8 bytes | ~ -9.22 × 10^18 to 9.22 × 10^18 | Large memory pointers, 64-bit API calls |
| Double | 8 bytes | ~ ±1.79 × 10^308 | High-precision decimals, large scientific math |
| Currency | 8 bytes | -922,337,203,685,477.5808 to … | Financial modeling (prevents rounding drift) |
The “Intermediate Calculation” Overflow Trap
A subtle way Error 6 occurs is during arithmetic on numbers that fit within an integer range, but whose intermediate product exceeds 32,767 before being assigned to a larger variable:
Dim totalSeconds As Long
Dim minutes As Integer
minutes = 1000
' Even though totalSeconds is Long, VBA treats (minutes * 60) as an Integer calculation!
' 1,000 * 60 = 60,000, which exceeds 32,767:
totalSeconds = minutes * 60 ' FAILS: Runtime Error 6
The Solution: Force VBA to evaluate the math using 32-bit arithmetic by casting one of the operands with CLng():
' Cast explicitly to Long:
totalSeconds = CLng(minutes) * 60 ' Succeeds: 60,000 fits comfortably in a Long
If your financial or engineering simulations encounter mathematical bounds errors during repetitive calculation cycles, see #NUM! in Mathematical Iterations: Fixing Excel’s Calculation Limits and VBA for Finance: Handling “Overflow” in large-scale monte carlo simulations..
Diagnostic Summary Table
| Error Code & Description | Common Root Cause | First-Line Fix |
|---|---|---|
| Runtime Error 424: Object Required | Missing Set keyword; misspelled UserForm control; referencing unloaded form. | Use Set obj = ...; use Me. in UserForm modules; verify control spelling. |
| Runtime Error 438: Object doesn’t support property/method | Typo in member name; calling a Range property on a Worksheet; late-binding mismatch. | Use early binding (Dim ws As Worksheet); verify object model hierarchy. |
| Runtime Error 6: Overflow | Counter or variable declared as Integer exceeding 32,767; intermediate math overflow. | Change Integer to Long; cast intermediate multiplication terms with CLng(). |
Next Diagnostic Steps
If your code executes without variable errors but freezes or runs out of execution memory on large loops, review Troubleshooting “Out of Resources” during massive VBA loops..
If your macros encounter type-related failures while reading cell values that contain non-numeric characters, see Runtime Error 13: Type Mismatch (Trying to perform math on a string variable)..
To trace and print variable allocations in real time before an overflow occurs, use the debugging workflow detailed in How to use the Immediate Window to debug variable values in real-time..