Runtime Error 11: Division by zero in VBA calculations.

VBA halts execution with Runtime Error 11: Division by zero when a line of code attempts to divide a numeric value by zero (0), or by an uninitialized variable that defaults to zero. Unlike worksheet formulas that display a graceful #DIV/0! cell error, VBA treats mathematical division by zero as an unhandled runtime exception that immediately stops macro execution.

Quick Answer

To fix Runtime Error 11, check that the divisor is not equal to zero before running the division operator (/ or \). Use an explicit If divisor <> 0 Then validation guard, handle empty or blank cells before reading them into variables, or set default fallback values when calculating ratios.


Why the Error Occurs

Division by zero is mathematically undefined. When VBA encounters a forward slash / (floating-point division) or backslash \ (integer division) where the denominator evaluates to zero, it raises Runtime Error 11:

Dim revenue As Double
Dim unitsSold As Long
Dim pricePerUnit As Double

revenue = 50000
unitsSold = 0

' Halts immediately with Runtime Error 11:
pricePerUnit = revenue / unitsSold

This error also occurs silently when:

  1. Uninitialized Variables Are Used: Numeric variables in VBA (Long, Integer, Double, Single) default to 0 upon declaration. If code divides by a variable before assigning it a value, it triggers Error 11.
  2. Blank Cells Are Treated as Zero: When reading an empty cell directly into a numeric calculation, VBA converts the blank cell to 0.
  3. Modulo Operations Fail: The Mod operator also checks for zero denominators. Writing x Mod 0 triggers Runtime Error 11.

For worksheet-level handling of zero-value denominators, see #DIV/0! in Financial Ratios: Zero-revenue scenarios and #DIV/0! in AverageIf/AverageIfs: When no criteria are met.


How to Fix Division by Zero

1. Add Conditional Guard Logic

The most dependable fix is a simple validation check before calculating:

Sub CalculateAverage()
    Dim totalCost As Double
    Dim itemCount As Long
    Dim unitCost As Double

    totalCost = Range("A2").Value
    itemCount = Range("B2").Value

    ' Guard clause prevents Error 11
    If itemCount <> 0 Then
        unitCost = totalCost / itemCount
        Range("C2").Value = unitCost
    Else
        Range("C2").Value = 0 ' Or "N/A", or leave blank
    End If
End Sub

2. Handle Floating-Point Rounding Near Zero

When working with Double or Single data types, floating-point math can result in extremely small numbers rather than an exact zero. If you calculate a denominator that might be infinitesimally small, use an epsilon check:

Const EPSILON As Double = 0.0000001

If Abs(denominator) > EPSILON Then
    result = numerator / denominator
Else
    result = 0
End If

For floating-point precision background in financial or operational models, refer to Floating Point Errors: Troubleshooting why (1.2 – 1.1) doesn’t equal 0.1 in Excel..