Type Mismatch (Error 13) occurs when you try to specify a value to a variable that doesn’t match with its data type. In VBA, when you declare a variable you need to define its data type, and when you specify a value that is different from that data type you get the type mismatch error 13.
In this tutorial, we will see what the possible situations are where runtime error 13 can occurs while executing a code.
Type Mismatch Error with Date
In VBA, there is a specific data type to deal with dates and sometimes it happens when you using a variable to store a date and somehow the value you specify is different.
In the following code, I have declared a variable as the date and then I have specified the value from cell A1 where I am supposed to have a date only. But as you can see the date that I have in the cell a one is not in the correct format VBA is not able to identify it has a date.
Sub myMacro() Dim iVal As Date iVal = Range("A1").Value End Sub
Type Mismatch Error with Number
You’re gonna have you can have the same error while dealing with numbers where you get a different value when you trying to specify a number to a variable.
In the following example, you have an error in cell A1 that is supposed to be a numeric value. So when you run the code, VBA shows you the runtime 13 error because it cannot identify the value as a number.
Sub myMacro() Dim iNum As Long iNum = Range("A6").Value End Sub
Runtime Error 6 Overflow
In VBA, there are multiple data types to deal with numbers and each of these data types has a range of numbers that you can assign to it. But there is a problem when you specify a number that is out of the range of the data type.
In that case, we will show you runtime error 6 overflow which indicates you need to change the data type and the number you have specified is out of the range.
Other Situations When it can Occurs
There might be some other situations that you could face the runtime error 14: Type Mismatch.
- When you assign a range to an array but that range only consists of a single cell.
- When you define a variable as an object but by writing the code you specify a different object to that variable.
- When you specify a variable as a worksheet but use sheets collection in the code or vice versa.
How to Fix Type Mismatch (Error 13)
The best way to deal with this error is to use to go to the statement to run a specific line of code or show a message box to the user when the error occurs. But you can also check the court step by step before executing it. For this, you need to use VBA’s debug tool, or you can also use the shortcut key F8.
There’s More VBA Errors
Subscript Out of Range (Error 9) | Runtime (Error 1004) | Object Required (Error 424) | Out of Memory (Error 7) | Object Doesn’t Support this Property or Method (Error 438) | Invalid Procedure Call Or Argument (Error 5) | Overflow (Error 6) | Automation error (Error 440) | VBA Error 400