

If you put the month in the wrong order or leave out the day, VBA will still do it’s best to accommodate you. VBA is very flexible when it comes to assigning a date to a date variable. L = "a" ' Double is a decimal number Dim d As Doubleĭ = "a" ' Currency is a 4 decimal place number Dim c As CurrencyĬ = "a" Dim d As Double ' Type mismatch if the cell contains text

Assigning a string to a numericĪs we have seen, trying to place text in a numeric variable can lead to the VBA Type mismatch error.īelow are some examples that will cause the error ' Sub TextErrors() The following sections show the different ways that the VBA Type Mismatch error can occur. The following video will show you how to find the exact piece of code that causes a VBA Error in under a minute: If the line of code is complex then it may not be obvious which part is causing the error. If your code has Error Handling then it may not be obvious which line has the error. The most important thing to do when solving the Type Mismatch error is to, first of all, locate the line with the error and then locate the part of the line that is causing the error. For these more complex cases we can use the Debugging tools to help us resolve the error.ĭon’t forget to check out my YouTube video on the Type Mismatch Error here: When a Type mismatch error occurs it is often not as simple as these examples. However, there are some conversions that VBA cannot do Dim x As Long ' Type mismatch errorĪ simple way to explain a VBA Type mismatch error, is that the items on either side of the equals evaluate to a different type.

Dim x As Long ' VBA will convert to integer 100 In general, VBA is very forgiving when you assign the wrong value type to a variable e.g. ' Declare a variable of type Date Dim ReportDate As Date ' Assigning a number causes a type mismatch error

If we try to place a non-date in this variable we will get a VBA Type mismatch error ' Sub TypeMismatchDate() This time we have a variable ReportDate of type Date. ' Declare a variable of type long integer Dim total As Long ' Assigning a string will cause a type mismatch error If we try to place text in the variable we will get the VBA Type mismatch error(i.e. Imagine we have a variable called Total which is a Long integer. The error appears as “run-time error 13 – Type mismatch”.įor example, if you try to place text in a Long integer variable or you try to place text in a Date variable. 3 How to Locate the Type Mismatch ErrorĪ VBA Type Mismatch Error occurs when you try to assign a value between two different variable types.
