You are on page 1of 1

ByRef argument type mismatch

Office 2013 and later

Contribute to this content


Use GitHub to suggest and submit changes. See our guidelines for contributing to VBA documentation.

An argument passed ByRef by reference, the default, must have the precise data type expected in the procedure. This error has the following cause and solution:

You passed an argument of one type that could not be coerced to the type expected.

For example, this error occurs if you try to pass an Integer variable when a Long is expected. If you want coercion to occur, even if it causes information to be lost, you can pas
parentheses. For example, to pass the Variant argument MyVar to a procedure that expects an Integer argument, you can write the call as follows:
unstlib

DimMyVar
MyVar=3.1415
CallSomeSub((MyVar))

SubSomeSub(MyNumAsInteger)
MyNum=MyNum+MyNum
EndSub

Placing the argument in its own set of parentheses forces evaluation of it as an expression. During this evaluation, the fractional portion of the number is rounded not truncat
argument type. The result of the evaluation is placed in a temporary location, and a reference to the temporary location is received by the procedure. Thus, the original

Note

If you don't specify a type for a variable, the variable receives the default type, Variant. This isn't always obvious. For example, the following code declares two variables, the f
AnotherVar, is an Integer.

VBA

DimMyVar,AnotherVarAsInteger

For additional information, select the item in question and press F1 in Windows or HELP on the Macintosh.

2016 Microsoft

You might also like