×

VBA Type Mismatch Error

What is a Type Mismatch Error?

VBA Type Mismatch Error is a run time error in excel, which often occurs when the data types contained in a VBA code are not matched in the correct order. This error is also known as run time error 13 or the number 13 error in the error’s category.

VBA Type Mismatch Error

In VBA, whenever this error occurs, a dialog box pops up stating as "run-time error 13: Type mismatch".

Reasons for its occurrence

Below are some causes for which the Excel type mismatch error generally occurs:

  1. The specified variable has been assigned to different data type and the data type is not matched in the correct order.
  2. If your Ms Excel application has damaged or has been installed incompletely.
  3. If there is any disagreement between the operating system and the Excel application
  4. If a user clicks on a missing menu function or a macro in an Excel file.
  5. If the malicious Virus/malware attack damages the Excel code
  6. Conflict with other programs while VBA Excel file is open

Advantages

  • The advantages of the Type mismatch error are as follows:
  • It alerts the user about the positioning of the mistake where it occurred in the VBA code.
  • The user gets to know about the point of error even before the compilation of the code.

Example

Code:

Sub TypeMisMatch_Example()
 'Declaring the variable
 Dim x As Byte
 'assigning a value to variable 'x'
 x = "Hello World"
 'using the message box to display the variable value
 MsgBox x
 End Sub 

Let’s us analyze step-step the above VBA code:

Step 1: Open the developer window by using the shortcut keywords Alt +F11.

Step 2: Create a module by right-clicking on the VBA Project-> Click on Insert-> Click on Module.

VBA Type Mismatch Error

Step 3: In the Module window, introduce the sub-block, followed by your macro name.

VBA Type Mismatch Error

Step 4: Declare your variable with the required data type. For instance, in the below reference, we have declared the variable ‘x’ as Byte.

VBA Type Mismatch Error

Step 5: Next, we must assign the value for the variable ‘x’. Unlike, we have assigned the value for ‘x’ as “Hello World!”.

VBA Type Mismatch Error

Step 6: the last step is to call the MsgBox so as we can store the value of x and can display variable content within it.

VBA Type Mismatch Error

Output

Step 7: Execute the above code either by pressing the F5 shortcut key or by clicking on the Run button.

Step 8: You will notice that the Excel VBA will throw a runtime error dialogue box stating: run-time error 13: Type mismatch".

VBA Type Mismatch Error

The reason for the above error is that the Byte data type cannot hold the string or text value (Byte x = “Hello Word”), so the VBA macro throws a Type Mismatch Error.

Debug

Step 9: In the Microsoft Visual Basic dialogue box, click on the Debug option.

VBA Type Mismatch Error

Step 10: The VBA will highlight the code consisting of the mismatch error.

VBA Type Mismatch Error

Step 11: Resolve it (with int variable x assign an integer value) and again run the program.

VBA Type Mismatch Error

Step 12: The program will run successfully, displaying the message box.

VBA Type Mismatch Error

Type Mismatch Example 2

Code:

Sub TypeMisMatch_Example2()
 'Declaring the variables
 Dim x As Byte
 Dim y As String
 Dim z As Integer
 'assigning the values to variables 'x' and 'y'
 x = 100 
 y = "Fifty"
 'adding both the variables
 z = x + y
 'using the message box to display the addition output
 MsgBox z
 End Sub 
VBA Type Mismatch Error

The above code would also throw a mismatch error as we are adding values of different data types (byte x+ string y). Thus, violating the VBA data type correct manner.

Output

VBA Type Mismatch Error

Debug

Debug and again run the code for error free output.

VBA Type Mismatch Error

Related Topics

Excel VBA Time Function

Excel VBA Time Function: The Time function in VBA returns the current time. Syntax Time () Parameter NA Return This function returns the current time.  Example 1 Sub TimeFunction_Example1() ' returns the current time Dim time_val...

1 minute read.

Excel VBA Round Function

VBA Round Function: The Round function in VBA rounds a number to a specified number of decimal places and returns the number. Syntax Round (Number, [NumDigitsAfterDecimal]) Parameter Number (required) –This parameter represents a numeric value one wants...

1 minute read.

Excel VBA CDate Function

VBA CDate Function: The CDate function in VBA converts an expression into a Date (or Time) data type. Syntax CDate (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to...

1 minute read.

Four VBA Clear methods

Four VBA Clear methods In Microsoft Excel, many times, a situation arises where the user wants to clear the data or any specific range of data. What if the user automates this task with...

6 minutes read.

Excel VBA Str Function

VBA Str Function: The Str function in VBA converts the given number into a string representation of that number. Syntax Str (Number) Parameter Number (required) – This parameter represents the numeric value that you want...

1 minute read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute read.

Excel VBA Sqr Function

VBA Sqr Function: The Sqr function in VBA returns the square root for the specified number. Syntax Sqr (Number) Parameter Number (required) – This parameter represents a positive numeric value that you want to calculate...

1 minute read.

Excel VBA Oct Function

VBA Oct Function: The Oct function in VBA converts the given number into octal notation and returns the result as a string. Syntax Oct (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.

Excel VBA Month Function

The Mont function in VBA returns the month number for the specified date. Syntax Month (Date) Parameter Time (required) – This parameter represents the date. Return This function returns the month number for the specified date. Example 1 Sub MonthFunction_Example1() ...

1 minute read.

Excel VBA Split Function

Excel VBA Split Function: The Split function in VBA is used to split a string into several substrings and return a one-dimensional array of substrings. Syntax Split (Expression, [Delimiter], [Limit], [Compare]) Parameter Expression (required) – This parameter...

2 minutes read.

Excel VBA Atn Function

VBA Atn Function: The Atn function in VBA returns the arctangent between quadrant -?/2 and +?/2 for the specified number, in radians. Syntax Atn (Number) Parameter Number (required) – This parameter represents the number that...

1 minute read.

VBA Color Index Property

What is Color Index Property? The Excel VBA Color Index is used to change the color for the cell or range of cells or text (located under the Font section). It sets the color...

5 minutes read.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

Excel VBA InStrRev Function

VBA InStrRev Function: The InStrRev function in Excel VBA returns an integer representing the position of a substring within the specified string if the substring is fount else it returns...

2 minutes read.

Excel VBA Hex Function

VBA Hex Function: The Hex function in VBA converts the given number into hexadecimal notation and returns the result as a string. Syntax Hex (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.

Excel VBA Minute Function

The Minute function in VBA returns the minute component for the specified time. Syntax Minute (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the minute component for the specified time. Example 1 Sub MinteFunction_Example1() ...

1 minute read.

Scope in Visual Basics

Definition of Scope The scope of any programming language implies the area of code where the variables will be identified, accessed, and used. Every variable has a scope associated with it. The scope of...

4 minutes read.

VBA Charts Basic Operations

VBA Charts- Basic Operations The Chart Object in Excel VBA represents the collection of all the charts sheet present in a workbook. A chart can be either an embedded chart or a separate chart sheet. The...

6 minutes read.

For Next loop in VBA

For Next Loop The ”For Next” loop is used for a fixed number of times. It works by implementing the loop for the specified number of times. In this, the user specifies how...

3 minutes read.

Excel VBA CDbl Function

VBA CDbl Function: The CDbl function in VBA converts an expression into a Double data type. Syntax CDbl (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.