Excel 尝试调用外部VBA函数时,只能强制在公共对象模块中定义的用户定义类型
我试图从Excel调用Access函数,但出现以下错误: 编译错误:只有在公共对象中定义的用户定义类型 模块可以强制到变量或从变量强制到变量,也可以传递到后期绑定 功能 我试着采用我发现的这个,但没有运气。这是我的密码: 在Excel模块的ExternalStatistics中Excel 尝试调用外部VBA函数时,只能强制在公共对象模块中定义的用户定义类型,excel,ms-access,vba,Excel,Ms Access,Vba,我试图从Excel调用Access函数,但出现以下错误: 编译错误:只有在公共对象中定义的用户定义类型 模块可以强制到变量或从变量强制到变量,也可以传递到后期绑定 功能 我试着采用我发现的这个,但没有运气。这是我的密码: 在Excel模块的ExternalStatistics中 Option Explicit Public Type MyExternalStatistics esMyInvites As Single esMyInvitePerTalk
Option Explicit
Public Type MyExternalStatistics
esMyInvites As Single
esMyInvitePerTalk As Single
End Type
Public MyExtRecStats As MyExternalStatistics
Option Compare Database
Option Explicit
Public Type MyExternalStatistics
esMyInvites As Single
esMyInvitePerTalk As Single
end Type
Public Function ExternalRecruitingStats(StatWindow As Integer) As MyExternalStatistics
Dim MyRecStats As MyExternalStatistics
Dim Invites As Integer, Talks As Integer
Invites = 1
Talks = 2
With MyRecStats
.esMyInvites = CSng(Invites)
.esMyInvitesPerTalk = CSng(Invites/Talks)
End With
ExternalRecruitingStats = MyRecStats 'return a single structure
End Function
在Sheet1(压缩数字)对象中:
在访问模块ExternalStatistics中
Option Explicit
Public Type MyExternalStatistics
esMyInvites As Single
esMyInvitePerTalk As Single
End Type
Public MyExtRecStats As MyExternalStatistics
Option Compare Database
Option Explicit
Public Type MyExternalStatistics
esMyInvites As Single
esMyInvitePerTalk As Single
end Type
Public Function ExternalRecruitingStats(StatWindow As Integer) As MyExternalStatistics
Dim MyRecStats As MyExternalStatistics
Dim Invites As Integer, Talks As Integer
Invites = 1
Talks = 2
With MyRecStats
.esMyInvites = CSng(Invites)
.esMyInvitesPerTalk = CSng(Invites/Talks)
End With
ExternalRecruitingStats = MyRecStats 'return a single structure
End Function
它不喜欢
MyExtRecStats=.Run(“ExternalRecruptingStats”,RecruiteWindow)
语句。我希望最终在访问函数中分配几个集合,并使用一个对象将它们全部返回。然后我可以将这些值放在电子表格中应该放在的位置。VBA中的类型定义是非常局部的,当您尝试将它们用于可能无法访问确切类型定义的对象时(这里可能就是这种情况)
有时,使用类
可能有效。在传递之前,您需要将类公开并实例化它,但我怀疑它是否真的能工作(原因与类定义在一个应用程序与另一个应用程序之间不可见相同)
另一个简单的解决方案是使用一个简单的集合
对象,将值作为项目添加到集合中。当然,添加/检索项目的确切顺序很重要
在本文中,对于类似的问题,有一些有趣的答案。它是关于VB6的,但它也应该在很大程度上适用于VBA
说到这里,您可以通过将Access代码导入Excel来解决所有问题。您可以使用Excel中的DAO或ADO操作Access数据库,就像在Excel中一样,例如: