如何在函数中使用用户定义类型?
How do I use a User Defined Type in a function?
以下简单示例不起作用:
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
如何"define"类型?
你的代码在我的 excel 2010
中编译得很好
无论如何,它对利用用户定义的类型毫无用处
下面是一个 "complete" 示例
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
以下简单示例不起作用:
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
如何"define"类型?
你的代码在我的 excel 2010
中编译得很好无论如何,它对利用用户定义的类型毫无用处
下面是一个 "complete" 示例
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