Runtime Error 6: Overflow (Variable value exceeds the Integer limit—use Long)

VBA Runtime Error 6 occurs when a macro attempts to store a value inside a variable that lacks the memory capacity to hold it, immediately halting code execution. In financial models, data processing pipelines, and daily macro operations, this unexpected crash leaves automated processes half-executed and can compromise data integrity if workbook updates are interrupted mid-script. Resolving this requires matching variable storage types to the scale of the calculated values.

Fast-Fix: The 45-Second Solution

VBA Runtime Error 6 happens when a variable or intermediate calculation exceeds its defined data type capacity—most commonly when a variable declared as Integer holds a value greater than 32,767. To fix it, open the Visual Basic Editor, find the failing variable declaration line, and change Dim i As Integer to Dim i As Long.

Quick Risk Snapshot

  • Severity Tier: Moderate to High (halts execution mid-routine, potentially corrupting uncommitted transactions).
  • Is it safe to ignore? No. The macro will immediately crash every time the boundary condition is met.
  • Most common cause: Counting rows beyond row 32,767 or multiplying two Integer values that exceed 32,767.
  • Rare/Serious cause: Nested loop multiplication overflowing byte or 32-bit Long buffers in recursive calculations.

What Escalates the Risk

Modern Excel datasets frequently exceed legacy limits. Prior to Excel 2007, worksheets contained 65,536 rows, but modern .xlsx and .xlsm workbooks contain 1,048,576 rows. Legacy macros written for older workbooks often use Integer declarations for row counters; running these macros on modern, larger datasets guarantees a crash at row 32,768.

Furthermore, running background macros without active error handling worsens the impact. When Application.ScreenUpdating = False or Application.DisplayAlerts = False is active when Runtime Error 6 triggers, Excel remains frozen without visual updates, masking partial data writes and forcing users to restart the application.

Common Confusion Fix

Runtime Error 6 is frequently confused with other common VBA execution errors due to similar diagnostic popups:

  • Runtime Error 6 (Overflow): Occurs when a numeric value is mathematically valid but exceeds the maximum storage capacity of the declared data type (e.g., placing 40,000 in an Integer).
  • Runtime Error 13 (Type Mismatch): Occurs when attempting an operation on incompatible data types, such as attempting mathematical calculations on text strings or mismatched object instances, see Runtime Error 13: Type Mismatch (Trying to perform math on a string variable).
  • Runtime Error 11 (Division by Zero): Occurs specifically when a mathematical denominator evaluates to zero or an uninitialized blank variable in a division statement, see Runtime Error 11: Division by zero in VBA calculations.

What To Do Right Now

  1. Click Debug on the Runtime Error 6 popup to highlight the exact line causing the overflow in the VBA editor.
  2. Identify all variables in that line and locate their Dim statements at the top of the procedure.
  3. Change the data type from Integer to Long (or Double / Currency if fractional numbers or very large financial amounts are involved).
  4. If the error occurs on an implicit calculation (e.g., Result = 300 * 200), force explicit type conversion using CLng() on at least one operand: Result = CLng(300) * 200.
  5. Press F5 to resume execution or Ctrl + Break to reset the procedure safely.

Hard-Stop Triggers

Stop execution and review the workbook state immediately if any of these conditions apply:

  • The macro crashed during a loop that executes Delete, ClearContents, or database write commands on worksheet rows.
  • Application.ScreenUpdating or Application.Calculation was set to False or Manual before the crash, leaving Excel un-rendered or uncalculated.
  • The code was executing an external API call, SQL transaction, or file export that remains unclosed.

Professional Audit Path

To prevent recurring overflow errors and establish code stability, follow this standard inspection protocol:

  1. Enforce Global Variable Declarations: Always include Option Explicit at the top of every module to ensure no variables default to untyped Variant or mismanaged implicit types, see “Variable not defined”: Why you must use Option Explicit.
  2. Audit All Row Counter Variables: Search the module for all Dim ... As Integer statements. Any variable tracking worksheet rows, loop indices, or array bounds should be declared As Long.
  3. Use the Immediate Window: During debugging, evaluate suspicious calculation components individually in the Immediate Window (Ctrl + G) by typing ? [expression] to pinpoint implicit coercion failures.
  4. Inspect High-Value Financial Calculations: For large financial operations or Monte Carlo simulations, verify whether 32-bit Long integers (limit ~2.14 billion) or 64-bit LongLong / Double types are required, see VBA for Finance: Handling “Overflow” in large-scale monte carlo simulations.

Final Calculation

VBA Runtime Error 6 is a straightforward memory capacity failure that occurs when numbers outgrow their assigned storage containers. Because modern Excel workbooks routinely process datasets far larger than 32,767 records, using Integer for row counters or integer arithmetic is a legacy practice that should be retired. Standardizing on Long for all integer variables and explicitly casting intermediate calculation operands guarantees that your macros process large datasets reliably without crashing.