Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Latest commit

 

History

History
47 lines (27 loc) · 1.93 KB

byref-argument-type-mismatch.md

File metadata and controls

47 lines (27 loc) · 1.93 KB
title keywords f1_keywords ms.prod ms.assetid ms.date
ByRef argument type mismatch
vblr6.chm1011308
vblr6.chm1011308
office
6adca657-8620-e3f1-3587-e317f988979c
06/08/2017

ByRef argument type mismatch

An argument passed ByRef (by reference), the default, must have the precise data type expected in theprocedure. 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 pass the argument in its own set of parentheses. For example, to pass the Variant argument MyVar to a procedure that expects an Integer argument, you can write the call as follows:

Dim MyVar 
MyVar = 3.1415 
Call SomeSub((MyVar)) 
 
Sub SomeSub (MyNum As Integer) 
MyNum = MyNum + MyNum 
End Sub
Placing the argument in its own set of parentheses forces evaluation of it as an [expression](vbe-glossary.md). During this evaluation, the fractional portion of the number is rounded (not truncated) to make it conform to the expected 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  `MyVar` retains its value.

 **Note**  If you don't specify a type for a [variable](vbe-glossary.md), the variable receives the default type,  **Variant**. This isn't always obvious. For example, the following code declares two variables, the first, `MyVar`, is a  **Variant**; the second, `AnotherVar`, is an  **Integer**.
Dim MyVar, AnotherVar As Integer 

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