简体   繁体   English

在Excel VBA上返回用户定义的类型

[英]Returning user-defined types on Excel VBA

I've been fiddling with Excel VBA for some time, but I never had any formal training whatsoever. 我已经摆弄Excel VBA已有一段时间了,但是我从未接受过任何正式的培训。 I have the following piece of code on a XLSM module : 我在XLSM模块上有以下代码:

Public Type ks_solution
    W As Integer
    NF As String
    ID As Integer
End Type

Public Sub MySub()
    //does some things
    MyKSSolution = MyFunction(params)
End Sub

Public Function MyFunction(params) as ks_solution
    //does stuff and returns a ks_solution
End Function

When I try to run the code, VBA compiler returns a "Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions" error. 当我尝试运行代码时,VBA编译器返回“只能在公共对象模块中定义的用户定义类型可以与变量强制转换或从变量强制转换或传递给后期绑定函数”错误。

Any help would be greatly appreciated. 任何帮助将不胜感激。

--Yuri -尤里

Er, nevermind. 嗯,没关系。 Looks like the problem was in the var declarations. 看起来问题出在var声明中。

I thought 我想

Dim v1, v2 as ks_solution

was the same as 与...相同

Dim v1 as ks_solution, v2 as ks_solution

but apparently, it isn't. 但显然不是。 In the first case, v1 gets declared as a Variant. 在第一种情况下,v1被声明为Variant。 Sorry to take your time. 抱歉,慢慢来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM