excel - 如何从 VBA 函数返回自定义类型

标签 excel vba

我想从 VBA 函数返回自定义类型。当我运行以下代码时,我收到错误“第 16 行错误 - 类型不匹配”。我真的不明白出了什么问题。

Type FullName
    FirstName As String
    LastName As String
End Type

Sub Main 
Dim fullName As FullName
fullName = GetName()

MsgBox fullName.FirstName &" "& fullName.LastName

End Sub

Function GetName() As FullName
    Dim temp As FullName
    temp.FirstName = "John"
    temp.LastName = "Doe"

    Set GetName = temp

End Function

最佳答案

尝试这个

Function GetName() As fullName
    Dim temp As fullName
    temp.FirstName = "John"
    temp.LastName = "Doe"

    With temp
        GetName.FirstName = .FirstName
        GetName.LastName = .LastName
    End With

End Function

但是GetName = temp应该也可以工作

关于excel - 如何从 VBA 函数返回自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57323491/

相关文章:

class - vba 错误 438 但对象类确实包含子

excel - 需要帮助修复我的代码以通过 vba 发送自动电子邮件

c# - References of References with COM 调用者

python - 我可以将 Datanitro 集成到可执行文件中吗?

asp.net - Excel VBA查询外部.aspx页面并检索数据

vba - 如何返回调用宏的工作表?

VBA Max() 的单元格地址

excel - 是否有用于将每个报表过滤器的数据透视表打印到组合 PDF 文件的宏

javascript - Office.Js Row单击事件

vba - Countif 不计算往年