quant
quant

Reputation: 23042

How do I use a User Defined Type in a function?

The following trivial example doesn't work:

Public Type MyType
    a As Double
    b As Integer
End Type

Function Test() As Variant
    Dim x As MyType
    Test = 2
End Function

Compile Error: User-defined type not defined

How do I "define" the type?

Upvotes: 1

Views: 68

Answers (1)

user3598756
user3598756

Reputation: 29421

your code compiles fine in my excel 2010

anyhow it'd be of no use for exploiting a user defined type

here follows a "complete" example

Option Explicit

Public Type MyType
    a As Double
    b As Integer
End Type

Sub main()
    Dim x As MyType

    'initialize fields of your type
    x.a = 1.2
    x.b = 1

    MsgBox Test(x)
End Sub


Function Test(x As MyType) As Variant

    Test = x.a + x.b

End Function

Upvotes: 1

Related Questions