Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Data Type as a Function argument

Status
Not open for further replies.

mintjulep

Technical User
Aug 20, 2004
1,552
JP
For readability and self-documentation I thought I'd try to use UDT's in some code.

I get the error "ByRef Argument Type Mismatch" and help isn't helpful, just telling me that data types need to match.

So, reduced to a trivial case for the purpose of trying to figure out what to do:

Code:
Public Type Voltage
value As Double
End Type
---------------
Public Function test(myinput As Voltage) As Voltage
test.value = myinput.value

End Function

To my mind this should work as I think I'm consistent with data types.

So what am I doing wrong?
 
Your problem is not the function, but likely how you are calling the function. So how are you calling it?
 
I have tried in the VBA editor immediate window and as a UDF in a worksheet.
 
Sorry. I mean what does you code look like that calls the function.

Here is an example where I call the function and return a new voltage and add 10 to the voltage I pass in.
Code:
Public Type voltage
  value As Double
End Type

Public Function Test(myVolt As voltage) As voltage
  Test.value = myVolt.value + 10
End Function

Public Sub TestTest()
  Dim myVolt As voltage
  Dim rtnVolt As voltage
  myVolt.value = 10
  rtnVolt = Test(myVolt)
  MsgBox rtnVolt.value
  MsgBox myVolt.value
End Sub
 
Humm.

Ok, your code sample works - within limits.

Call "testtest" from the immediate window and the expected two message boxes appear.

But "? test(10)" in the immediate window results in the "ByRef Argument Type Mismatch" error.

=test(10) in a spreadsheet cell as a UDF results in #VALUE!.

So it works when called from inside a sub, but nowhere else?

Curious and annoying.

 
Sorry my code work everywhere. It does not work the way you are calling it, because the way you are calling does not make any sense. Of course this does not work. And of course it gives you a type mismatch.
?test(10)
Bou have to pass in a voltage data type, but you are passing an integer. If you want to pass in an integer and return a voltage data type with that value then

Public Function Test(voltValue as integer) As voltage
Test.value = voltValue
End Function

Then in the immediate window
?Test(10).value

This would not make sense anyways
?Test(10)
What property would you expect that to print.

What are you trying in the big picture, because these examples do not seem to make sense.
 
>But "? test(10)" in the immediate window results in the "ByRef Argument Type Mismatch" error

Why would you expect this to work? VB cannot guess that you want this to be a voltage. It uses some built-in rules (using built-in types) to decide what kind of type you mean here, and goes with Integer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top