Error: ByRef argument type mismatch

Hello all,
I am getting the message in the IDE "ByRef argument type mismatch"  In Module1 I am calling a function in Module2 as shown.  I hope I have included enough code.  



   Dim Mask$,dblVar#
   Result$ = Ptformat(Mask,dblVar)


Public Function PTFormat(ByVal strMask, ParamArray   FParams() As Variant) As String
   iMaxNum = UBound(FParams)
   ReDim vDataParams(iMaxNum)
   For i = 0 To iMaxNum
       vDataParams(i) = FParams(i)

strResult = sProcessFormat(strMask, vDataParams())

End Function

Function sProcessFormat(strWorking As String, vDataParams() As Variant) As String


I get the error when I call sProcessFormat in Module2.  It is complaining about the vDataParams parameter.

The code in Module2 works in other projects.  I have one project in which I cannot make it work (although it worked at one time in the project in which it is failing now).

I suspect something got set up wrong somewhere.  Any ideas?
Who is Participating?
Dave_GreeneConnect With a Mentor Commented:
Does this work?

Public Function PTFormat(ByVal strMask, ParamArray   FParams() As Variant) As String

...  I don't think so

If you pass a parameter to a function or sub procedure by reference, the type of the actual parameter passed and the corresponding function argument must match. Otherwise, you will get a "ByRef argument type mismatch" error.

The reason types have to match with ByRef parameters is that the called procedure is working on the original outside variable through a reference pointer. In the following step-by-step example, if the procedure thinks it is modifying a variant but the outside variable is really a control, the data will probably be ruined.

If something is passed by value, Visual Basic can do automatic type conversion. When you pass by value, the inside procedure is working on a copy and can therefore modify it in any way, such as converting the passed object to a temporary Variant and working on that.

This applies to simple built-in types as well as objects. Problems like this are easier to understand and debug if you set Option Explicit and declare every variable type explicitly.
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Change this

Function sProcessFormat(strWorking As String, vDataParams() As Variant) As String


Function sProcessFormat(strWorking As String, vDataParams As Variant) As String

GeneMAuthor Commented:
Loranger & Dave,
Thank you both for responding.

Loranger, I have Option Explicit turned on and have all data types explicitly defined.  I just left some of the definitions out of the sample code.

Dave, I tried your first suggestion and it doesn't help.  As to your second question (Does this work?) the answer is yes.  The Keyword ParamArray is used to indicate that the final argument is an Optional array of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments.

Unless I am really cracking up :-), the code in Module2 works correctly in other projects.  Thus, I think it has something to do with my current project.
No, actually you don't have *all* your variables defined. The header for PTFormat has an implicit Variant for it's first parameter (strMask).

Public Function PTFormat(ByVal strMask, ParamArray FParams() As Variant) As String

You are then passing this variant to the sProcessFormat function:

strResult = sProcessFormat(strMask, vDataParams())

Which has a ByRef string!:

Function sProcessFormat(strWorking As String, vDataParams() As Variant) As String

Either change PTFormat's header to this:

Public Function PTFormat(ByVal strMask As String, ParamArray FParams() As Variant) As String

Or change sProcessFormat's header to this:

Function sProcessFormat(ByVal strWorking As String, vDataParams() As Variant) As String

GeneMAuthor Commented:
Thanks to all who responded.  I have found my problem.

I am updating a very large program that was written by someone else.  It contains about 50 BAS modules, each with many subs in them.

In one of the 50 BAS modules, I found a IsNumeric function!  The compiler was using that function instead of the VB IsNumeric function.  

Again, thank you all for responding.  This problem was driving me crazy.  I thought sure it had something to do with my current project options.  Of course, it was related to my current project, as that is the only project in which I include the BAS module with the IsNumeric function.

I am giving the points to Dave just because he was early to post an answer, and gave me something to investigate!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.