简体   繁体   中英

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. I have the following piece of code on a XLSM module :

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.

Any help would be greatly appreciated.

--Yuri

Er, nevermind. Looks like the problem was in the var declarations.

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. Sorry to take your time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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