ByRef Argument type mismatch in Excel VBA?
In Excel VBA, the “ByRef argument type mismatch” error occurs when a procedure or function is called with an argument that is not the exact data type as the parameter of the procedure or function, and the argument is passed by reference (using the keyword “ByRef”).
For example, consider the following code. In this code, the “test” procedure takes a string parameter which is passed by reference, but in the “main” sub, an integer variable “num” is passed as an argument to the “test” procedure. Since “num” is not a string, the VBA ByRef Argument Type Mismatch error occurs.

If the code is run, you get the following error:

To fix this error, you must ensure that the data type of the argument being passed matches the data type of the procedure or function parameter. In this case, you could change the data type of the “num” variable to a string, and execute the code.


Table of Contents
What does ByRef mean?
In VBA (Visual Basic for Applications), ByRef means “by reference.” When a parameter is passed to a subroutine or function by reference, it receives a reference to the original variable in memory rather than a copy of its value.
It means any changes made to the parameter inside the subroutine or function will also affect the original variable outside of it. On the other hand, when a parameter is passed by value, a copy of the variable’s value is passed to the subroutine or function, and any changes made to the parameter inside the subroutine or function do not affect the original variable outside of the subroutine or function.
By default, VBA passes parameters by reference, so the keyword “ByRef” is not. However, you can explicitly declare a parameter to be passed by reference using the “ByRef” keyword in the parameter list.
Here’s an example of a subroutine that accepts a parameter by reference:

In this example, the “str” parameter is passed by reference using the “ByRef” keyword.
When the subroutine is called with a string variable as an argument, the string is initialized onto the new variable, and the new string is printed.
It prints the output in the immediate tab by declaring another subroutine that utilizes it.

Which will result in the output:

Top 3 reasons for VBA ByRef Argument type Mismatch
Let us look at the top reasons for the VBA ByRef argument type mismatch error:
#1 – Data Type Mismatch: The most common reason for the “ByRef argument type mismatch” error is a VBA variable data type mismatch between the argument passed to the subroutine or function and its expected parameter type.
For example, suppose a subroutine expects a string parameter, but an integer is passed to it. In that case, it will mean that there is a VBA ByRef argument type mismatch integer, causing the “ByRef argument type mismatch” error will occur.
#2 – Null Values: Another common cause of the “ByRef argument type mismatch” error is when null values are passed as arguments to the subroutine or function.
Some data types, such as dates or numeric values, cannot handle null values, and attempting to pass them as arguments can result in the “ByRef argument type mismatch” error.
#3 – Incorrect Function Call: The “ByRef argument type mismatch” error sometimes occurs due to incorrect function or subroutine calls. For example, if a function is called with too few or too many arguments or if the arguments are in the wrong order, the “ByRef argument type mismatch” error can occur.
It’s essential to ensure that the function or subroutine is called with the correct number of arguments and in the proper order.
By identifying the specific reason for the “ByRef argument type mismatch” error, you can correct the issue and prevent it from happening again. Now let us look at some examples of why this error occurs.
Error Reason #1 – Different Variable Names
Suppose you receive a “ByRef argument type mismatch” error in VBA. In that case, it typically means that the data type of the argument you are passing to a subroutine or function does not match the data type of the parameter declared in the subroutine or function header.
However, it’s also possible to get this error even if their data types match but the variable names differ. For example, it can happen when you pass a variable to a subroutine or function using a different name than the one used in the subroutine or function header.
Consider the following code:

In this code, the MultiplyNumberByThree() subroutine references a variable ‘x’ of integer data type, which returns triple the input number provided.
But, when we move to the subroutine PrintProduct(), we can see the wrong variable ‘num’ being referenced.
It will print the VBA ByRef argument type mismatch array:

This error can be fixed by ensuring the variable used in a subroutine or function should match the parameter name declared in the function header. Hence, the correct code for the above example will be:

Code:
Sub MultiplyNumberByThree(ByRef x As Integer)
x = x * 3
Debug.Print x
End Sub
Sub PrintProduct()
Dim no As Integer
no = 10
MultiplyNumberByThree no
End Sub
Here, ‘num’ was changed to ‘no’ as defined in the subroutine, eliminating the error. Now the output will be printed in the Immediate tab as:

Error Reason #2 – Different Variable Data Types
If you pass a variable of a different data type than the parameter declared in a subroutine or function, you will encounter a “ByRef argument type mismatch” error in VBA.
For example, consider the following code:

In this code, the subroutine MultiplyNumberByThree() expects an integer argument. Still, in the PrintProduct() subroutine, we get a variable ‘no’ with a String argument instead of an Integer type argument.
As a result, we will encounter a “ByRef argument type mismatch,” as shown below:

To fix this error, you should ensure that the variable’s data type passed to the subroutine or function matches the data type of the parameter declared in the subroutine or function header. So, in the example above, the correct code would be:

Here, the variable’s datatype in PrintProduct() is changed to Integer, matching the variable datatype argument needed for the subroutine MultiplyNumberByThree(), which will prevent the error from occurring. It will print the output in the Immediate tab.

Code:
Sub MultiplyNumberByThree(ByRef x As Integer)
x = x * 3
Debug.Print x
End Sub
Sub PrintProduct()
Dim no As Integer
no = 10
MultiplyNumberByThree no
End Sub
Error Reason #3 – Variable Data Types Missing in One Macro
Suppose a variable is declared in one macro in VBA but not declared or has a different data type in another macro. Then, when passing the variable between the two macros, you can encounter a “ByRef argument type mismatch” error.
For example, consider the following code:

The subroutine MultiplyNumberByThree() in the above code expects an integer as an argument. But, in the subroutine ExampleFunction(), we have not provided any declaration but just assigned a value to the variable. So, it will trigger a the ByRef error.

To fix this error, you should ensure that the variable is declared with the same data type in both macros. Add the “Option Explicit” word at the top to make the variable declaration mandatory.

In this code, both the variables are declared with the same data type in both macros, so the “ByRef argument type mismatch” error will not occur.
Which will print the output:

Code:
Sub ExampleFunction()
Dim x As Integer
x = 20
MultiplyNumberByThree x
MsgBox x
End Sub
Sub MultiplyNumberByThree(ByRef x As Integer)
x = x * 3
Debug.Print x
End Sub
How to Fix ByRef Argument Type Mismatch in Excel VBA?
To fix a “ByRef argument type mismatch” error in Excel VBA, you should first check if the data types of the argument you are passing to a subroutine or function match the data type of the parameter declared in the subroutine or function header.
Here are the steps that can be taken to prevent it, as shown below:
- Check that the data types of the argument and the parameter match.
For example, if the parameter is declared an Integer, ensure you pass an Integer as the argument. - Check that the variable name used in the subroutine or function call matches the parameter name declared in the subroutine or function header.
- Declare variables with the appropriate data type in each macro where the variables are used. It helps ensure that variables are passed correctly between macros.
- Use Option Explicit to ensure that all variables are declared before use.
- Pass the variable’s value instead of passing it ByRef if the variable is not used for output.
If the variable is used for output, ensure that it is declared and initialized correctly before passing ByRef.
Following these steps, you can identify and fix your Excel VBA code’s “ByRef argument type mismatch” error.
Important Things To Note
- Data types: Make sure that the data type of the argument being passed to the subroutine or function matches the data type of the parameter declared in the subroutine or function header.
- Variable names: Ensure that the variable name used in the subroutine or function call matches the parameter name declared in the subroutine or function header.
- Variable declaration: Declare variables with the appropriate data type in each macro where the variables are used.
ByRef vs. ByVal: Understand the difference between passing variables ByRef and ByVal. ByRef means that the memory address of the variable is passed to the subroutine or function, whereas ByVal implies that the variable’s value is passed. - Variable initialization: Ensure that the variable is declared and initialized correctly before it is passed ByRef.
- Error handling: Use error handling techniques such as “On Error Resume Next” or “On Error GoTo” to catch and handle errors in your code.
Frequently Asked Questions (FAQs)
A “Type mismatch” error can occur when using the Split function in VBA if the data type of the variable you are trying to split is incompatible with the data type expected by the Split function.
Error 3464 “Data Type Mismatch” is a standard error in VBA when assigning or comparing two values of incompatible data types. This error can occur in various situations in VBA code, but it often arises when working with data stored in databases or spreadsheets.
“Compile Error: Type Mismatch” is a VBA error that occurs when there is a mismatch between the data types of two values in the code. This error occurs at compile-time rather than at run-time, which means that the error is detected when the VBA code is being compiled rather than when it is being executed.
Recommended Articles
This has been a guide to VBA ByRef Argument Type Mismatch in excel VBA. Here we look into the top three reasons for the occurrence of error with examples. You may learn more from the following articles –
Leave a Reply