How to Fix Excel VBA Run-Time Error 13 Type Mismatch

When running macros in Microsoft Excel via Visual Basic for Applications (VBA), scripts frequently halt abruptly with: “Run-time error ’13’: Type mismatch”. This error occurs when a variable, object, or calculation receives a data type that does not match its declared expectation, such as assigning a text string to an integer variable or processing worksheet formula errors like #N/A as numbers.

Validate Worksheet Cell Contents for Formula Errors

Attempting to assign cell values that contain worksheet error states (such as #VALUE!, #DIV/0!, or #N/A) to standard VBA variables instantly provokes Error 13.

  1. Press Alt + F11 to open the VBA Editor and click Debug on the failing code line.
  2. Identify which cell is being read (e.g. myVar = Range("A1").Value).
  3. Wrap cell reads with IsError validation to handle spreadsheet errors gracefully:
    If Not IsError(Range("A1").Value) Then
        myVar = Range("A1").Value
    Else
        myVar = 0
    End If
  4. Using the Variant data type for reading raw cell contents prevents unexpected crashes when encountering diverse data formats.

Use Safe Type Conversion Functions (CStr, CInt, CDbl)

Implicit type casting can fail when processing international currency symbols, dates, or decimal separators.

  • Verify input values with IsNumeric or IsDate before executing mathematical operations:
    If IsNumeric(cellValue) Then
        totalAmount = CDbl(cellValue) * taxRate
    End If
  • Replace implicit string concatenation with explicit CStr() calls to avoid ambiguities between addition operators and string joins.

Avoid Assigning Multi-Cell Ranges to Scalar Variables

Referencing multiple cells without an array variable triggers an immediate type mismatch.

  1. Writing Dim val As String: val = Range("A1:A5").Value fails because Range("A1:A5").Value returns a two-dimensional array.
  2. Declare the receiving variable as an array or Variant: Dim valArr As Variant: valArr = Range("A1:A5").Value.
  3. Iterate through the array elements using For i = 1 To UBound(valArr, 1).

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.