Excel VBA Run Time Error 13 Type Mismatch Fix
In this article
Run-time error 13 'Type mismatch' in Excel VBA means your code tried to put a value of one data type into a variable or property that expects another — for example, assigning text to an Integer, comparing a number to a string, or passing a Range where a String is required. The fix is to find the line that triggers the error (use Debug > Compile and step through with F8), then either convert the value with the right function (CInt, CLng, CStr, IsNumeric) or change the variable's declared type to Variant so it can hold anything. Below is the full process, from locating the fault to preventing it.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- The workbook containing the macro, saved as .xlsm or .xlsb so VBA code is preserved.
- Basic familiarity with the VBA editor (Alt+F11) and the Immediate Window (Ctrl+G).
Step-by-Step
- Reproduce the error and note the highlighted line. Run the macro. When the error dialog appears, click Debug. The VBA editor opens with the offending line highlighted in yellow. If the line is a call to another procedure, the actual mismatch may be inside that procedure — click Debug > Step Into (F8) to follow the call.
- Compile the project first. In the VBA editor, choose Debug > Compile VBAProject. A compile error often points to a declared type conflict before runtime. Fix any compile errors before running again.
- Identify the data types on the failing line. Hover over variables to see their current values, or type
?VarNamein the Immediate Window and press Enter. Compare what the variable is declared as (e.g.,Dim qty As Integer) with what is actually being assigned (e.g., a cell containing "N/A" or an empty string). - Check the source of the value. If the value comes from a cell, confirm the cell is not blank, not text, and not an error value like #N/A. Use
IsNumeric(Range("A1").Value)in the Immediate Window to test. A blank cell read into a numeric variable is a very common cause. - Convert explicitly instead of relying on implicit conversion. Replace direct assignments with conversion functions:
CInt,CLng,CDbl,CDate,CStr, orCBool. Wrap them in validation, e.g.If IsNumeric(cell.Value) Then qty = CInt(cell.Value) Else qty = 0. This turns a crash into a handled case. - Change the variable type to Variant if the value's type is genuinely unknown. Declaring
Dim v As Variantaccepts numbers, text, dates, errors, and Empty. Use Variant for values read straight from worksheets, then convert only when you need to calculate. Avoid Variant for loop counters and fixed-size logic where a specific type is safer. - Fix comparisons and concatenations. Error 13 also appears when comparing mismatched types, e.g.
If Range("A1").Value = 5 Thenwhere A1 holds text "5". Convert one side:If CStr(Range("A1").Value) = "5" ThenorIf Val(Range("A1").Value) = 5 Then. For concatenation, use&rather than+, because+between a string and a number can raise the error. - Check object assignments. Assigning
Set rng = Range("A1").Valueis wrong because.Valuereturns a value, not an object. UseSet rng = Range("A1")or dropSetand assign the value to a non-object variable. - Re-run and step through with F8. Press F8 to execute one line at a time. When the yellow line stops advancing, you have found the exact statement. Fix it, then run the whole macro again from the top.
- Add error handling for the future. At the top of the procedure, add
On Error GoTo Handlerand at the bottom a handler that logsErr.Number,Err.Description, and the line viaErl(requires line numbers). This makes the next mismatch easy to trace instead of a mystery dialog.
Common Mistakes
- Assuming the highlighted line is the cause. It is often the line that receives a bad value from a previous line; step back one or two statements.
- Using
On Error Resume Nextto silence the error. The mismatch still corrupts results; it just hides the symptom. - Declaring everything as Variant to avoid the error. This works but hides real bugs and slows loops; convert deliberately instead.
- Forgetting that an empty cell read into a numeric variable yields Empty, which can trigger error 13 in arithmetic or comparison.
- Mixing
+and&for strings. Use&for concatenation. - Passing a Range object to a procedure that expects a String or Double. Pass
rng.Valueorrng.Textinstead.
Tips
- Use
TypeName(variable)in the Immediate Window to see what a Variant actually holds at runtime. - Prefer
CLngoverCIntfor row numbers and counts; Long avoids overflow on large sheets. - Validate worksheet input at the boundary — the moment data enters your code — rather than deep inside calculations.
- Keep
Option Explicitat the top of every module so undeclared variables are caught at compile time. - When reading dates, remember that
CDatedepends on regional settings; test with the same locale as your users.
Troubleshooting
If the error persists after conversion, check whether the failing value is an Excel error value such as #N/A or #VALUE!. Test with IsError(cell.Value) before converting. If the error appears only on some machines, suspect locale differences in decimal separators or date formats — use Application.International or parse with CDbl after replacing separators. If the error occurs inside a loop, log the loop index and the offending cell address to a spare sheet so you can see which row breaks. If the macro runs fine when stepped through but fails when run normally, look for uninitialised variables or code that depends on the active sheet — qualify every Range with its worksheet. If none of this isolates the line, temporarily replace conversions with MsgBox TypeName(value) to inspect the actual type at runtime.
Frequently Asked Questions
What does error 13 mean in simple terms?
Your code tried to use a value as a type it is not — for example, treating the text "abc" as a number.
Why does it happen only with some cells?
Those cells contain text, blanks, or error values where your code expects a number or date. Validate each cell before converting.
Can I just disable error checking?
You can, with On Error Resume Next, but the mismatch will still produce wrong results silently. Fix the type instead.
How do I find the exact line?
Click Debug on the error dialog, then press F8 to step line by line until execution stops advancing.
Does Option Explicit prevent error 13?
No, but it catches undeclared variables at compile time, which removes one common source of type confusion.
What is the quickest permanent fix?
Convert values explicitly with CInt, CLng, CDbl, or CStr at the point they enter your code, and keep Variant only for raw worksheet reads.