VBA Run-time Error 13 (Type Mismatch) โ Complete Fix
When VBA refuses to treat a value the way your code assumes it should โ here's how to find the mismatch and fix it safely.
Table of Contents
- 1. What Type Mismatch Actually Means
- 2. Interactive Demo: Type Checker
- 3. Cause 1: Text-Formatted Cells Used in Math
- 4. Cause 2: Assigning Objects Without Set
- 5. Cause 3: Null and Empty Value Confusion
- 6. Cause 4: Comparing Incompatible Variant Types
- 7. Safe Conversion Pattern
- 8. Frequently Asked Questions
1. What Type Mismatch Actually Means
VBA is loosely typed in many contexts (especially with the Variant data type), which can create a false sense that it will automatically convert any value into whatever type your code needs. Error 13 fires the moment that assumption breaks โ when code performs an operation that genuinely requires a specific type, and the actual value can't be automatically converted to satisfy it.
2. Interactive Demo: Type Checker
See which operations succeed or fail depending on a value's actual underlying type.
Try "1,024" with direct addition (fails on the comma), then switch to the CDbl version to see the explicit conversion succeed.
3. Cause 1: Text-Formatted Cells Used in Math
The most common trigger โ reading a cell's value and performing arithmetic directly on it, when the cell actually contains text (often from a CSV import preserving leading zeros, or a thousands-separator comma that VBA's implicit conversion can't automatically strip):
total = total + Range("A1").Value ' Error 13 if A1 contains "1,024" as text
total = total + CDbl(Range("A1").Value) ' Fails the same way โ CDbl also can't parse commas directly
total = total + CDbl(Replace(Range("A1").Value, ",", "")) ' Correct โ strip the comma first
4. Cause 2: Assigning Objects Without Set
In VBA, assigning an object reference (like a Range, Worksheet, or Workbook) requires the Set keyword. Omitting it is a classic source of Type Mismatch:
Dim rng As Range
rng = Range("A1:B10") ' Error 13 - missing Set keyword
Set rng = Range("A1:B10") ' Correct
5. Cause 3: Null and Empty Value Confusion
A cell with genuinely no content returns Empty in VBA, not Null โ attempting to explicitly compare a Variant against Null when the actual value is Empty (or vice versa) can trigger Type Mismatch in certain comparison contexts, particularly when interacting with database recordsets (via ADO) where Null and Empty are treated as distinctly different states, unlike in plain worksheet cell references.
6. Cause 4: Comparing Incompatible Variant Types
Variant variables can silently hold wildly different underlying types across different points in your code's execution. Comparing a Variant currently holding a Date against one holding a String, for example, can throw Type Mismatch even though both are declared simply as Variant:
Dim v1 As Variant, v2 As Variant
v1 = Now()
v2 = "some text"
If v1 = v2 Then ... ' Error 13 - comparing a Date-typed Variant against a String-typed Variant
7. Safe Conversion Pattern
The most robust defense against error 13 when reading worksheet values into VBA is to always check before converting, rather than assuming the type:
Dim cellVal As Variant
cellVal = Range("A1").Value
If IsNumeric(cellVal) Then
total = total + CDbl(cellVal)
Else
MsgBox "Cell A1 contains a non-numeric value: " & cellVal
End If
This pattern catches the mismatch proactively with a clear diagnostic message, rather than letting the macro halt abruptly with a generic error dialog.
8. Frequently Asked Questions
What causes VBA Run-time error 13 Type Mismatch?
Type Mismatch occurs when VBA code tries to use a value in a way that's incompatible with its actual data type, such as attempting numeric math on a cell containing text, assigning an object to a variable declared as a simple data type, or comparing incompatible variable types directly.
Why does error 13 happen with cell values that look like numbers?
A cell can display digits while its underlying value is stored as text rather than a true number, especially after CSV imports or manual typing with a leading apostrophe. VBA does not automatically convert this text to a number for arithmetic operations, causing a type mismatch.
How do I safely convert a cell value to a number in VBA?
Use IsNumeric to check the value first, then apply CDbl or Val to convert it safely, rather than assuming every cell already contains a true numeric type. Wrapping the conversion in an IsNumeric check prevents error 13 from occurring on unexpected text values.
Related Guides
- VBA Run-time Error 1004
- VBA Run-time Error 9 (Subscript Out of Range)
- Browse the Full 266+ Error Directory
Editorial Disclaimer: This guide is developed to the best of our domain knowledge and tested against Excel 2019+, Microsoft 365, and Google Sheets. Content is updated continuously as spreadsheet calculation engines evolve.