VBA raises Runtime Error 13: Type Mismatch when an operation receives an argument whose data type cannot be converted to the required type. In mathematical operations, this happens when code attempts arithmetic (+, -, *, /) using a variable, cell value, or array item that contains non-numeric text, symbols, spaces, or an underlying worksheet error like #VALUE! or #N/A.
Quick Answer
Resolve Runtime Error 13 by testing variables with
IsNumeric()before doing math, stripping out currency symbols or spaces usingReplace(), avoiding arithmetic directly on rawRange.Valuewithout checking for worksheet errors withIsError(), and converting strings to explicit numeric data types usingCDbl()orCLng().
How Type Mismatch Occurs in Arithmetic
VBA is a loosely typed language by default. If you add a string containing numeric characters to an integer, VBA will attempt to coerce the string into a number:
Dim x As Integer
Dim y As String
y = "20"
x = y + 5 ' Works: VBA automatically converts "20" to 20; x becomes 25
However, if the string variable contains any character that the Windows locale engine cannot interpret as a valid number, coercion fails immediately:
Dim x As Integer
Dim y As String
y = "20 " ' Note the trailing space
x = y + 5 ' FAILS: Runtime Error 13 (Type Mismatch)
Common real-world triggers include:
- Strings containing currency symbols, percentage signs, or thousand separators:
"$100.00","15%". - Cell values containing invisible non-breaking spaces (
Chr(160)) imported from ERP systems or web scrapers. - Cells containing worksheet errors: If cell
A1displays#N/Aor#VALUE!, readingRange("A1").Valuereturns anErrorsubtype, not a string or number. EvaluatingRange("A1").Value * 2instantly triggers Error 13.
Diagnostic Flow and Solutions
Arithmetic statement throws Error 13
│
├── Does the target cell contain a worksheet error (#N/A, #VALUE!)?
│ ├── YES ──> Check with IsError() before reading value.
│ └── NO
│
├── Does the variable contain text, spaces, or symbols?
│ ├── YES ──> Clean with Trim(), Replace(), and validate with IsNumeric().
│ └── NO
│
└── Is the destination variable too small (e.g., Integer holding >32,767)?
└── YES ──> Declare as Long or Double to prevent overflow.
1. Sanitize Strings Before Arithmetic
Never perform math directly on unvalidated text variables. Sanitize the string and confirm it is numeric first:
Sub SafeCalculation()
Dim rawInput As String
Dim cleanNum As Double
rawInput = "$1,250.50 "
' Strip currency symbols, commas, and regular spaces
rawInput = Replace(rawInput, "$", "")
rawInput = Replace(rawInput, ",", "")
rawInput = Trim(rawInput)
If IsNumeric(rawInput) Then
cleanNum = CDbl(rawInput)
MsgBox "Result: " & (cleanNum * 1.1)
Else
MsgBox "Cannot process input: " & rawInput, vbExclamation
End If
End Sub
2. Protect Code Against Cells with Formula Errors
When iterating through a range of cells, always check for formula error values before reading numeric content:
Sub SumRangeSafely()
Dim cell As Range
Dim total As Double
total = 0
For Each cell In Worksheets("Data").Range("A1:A50")
' Avoid Error 13 by checking if the cell holds a formula error
If Not IsError(cell.Value) Then
If IsNumeric(cell.Value) And Not IsEmpty(cell.Value) Then
total = total + cell.Value
End If
End If
Next cell
End Sub
If your worksheet formulas produce errors that break downstream macros, consult #VALUE! in Arithmetic: Mathematical operations on Cells with Spaces and #N/A when searching Numbers stored as Text.
3. The + Operator vs. the & Operator
In VBA, the plus symbol (+) serves as both an arithmetic operator and a legacy string concatenation operator. If you mix strings and numbers using +, VBA tries to guess your intent:
Dim a As Variant, b As Variant
a = "10"
b = 20
' If VBA tries to add them arithmetically and encounters non-digits, it crashes.
' Always use & for strings, and use + strictly for numbers:
Dim strResult As String
strResult = CStr(a) & CStr(b) ' Returns "1020" reliably
Dim numResult As Double
numResult = CDbl(a) + b ' Returns 30 reliably
Diagnostic Summary Table
| Error Scenario | Root Cause | First-Line Fix |
|---|---|---|
| “The file could not be accessed” | Network timeout, dropped VPN link, or sleeping mapped drive letter. | Convert mapped drive paths to direct UNC paths (\\Server\Share); implement an automated retry loop. |
| “SaveAs method… failed” | FileFormat mismatch with extension, or unhandled overwrite alerts. | Explicitly match the FileFormat enumeration (e.g., xlOpenXMLWorkbookMacroEnabled for .xlsm); set DisplayAlerts = False. |
| Type Mismatch (Error 13) | Performing arithmetic on strings containing letters, symbols, spaces, or formula errors. | Verify inputs using IsError() and IsNumeric(); strip non-digit characters and cast with CDbl(). |
Next Diagnostic Steps
If your data types appear correct but calculations still fail, you may have reached the capacity limits of the chosen numeric type; see Runtime Error 6: Overflow (Variable value exceeds the Integer limit—use Long)..
If your macros encounter unexpected text strings when reading from database or API sources, review Expression.Error: We cannot convert Text to Number (Handling “Dirty” data)..
To trace exactly how variables mutate before triggering Type Mismatch errors, set up a watch window following the guidelines in Using Watch Windows to track Object states..