[英]Compile Error: User-defined type not defined Access/Excel Reference
I receive the message Compile Error: User-defined type not defined
in a function when declaring an Excel object. 声明Excel对象时,我收到消息
Compile Error: User-defined type not defined
函数中Compile Error: User-defined type not defined
。 I have my reference set to Microsoft Office Object Library 14.0. 我的参考设置为Microsoft Office对象库14.0。
Function GetAIRR(arrAssCF, arrAssDate, Guess) As Double
Dim objExcel As Excel.Application
Set objExcel = New Excel.Application
Target = objExcel.Application.Xirr(arrAssCF, arrAssDate, Guess)
End Function
Reference the Microsoft Excel type library (ie not the Microsoft Office type library). 引用Microsoft Excel类型库(即不是Microsoft Office类型库)。
You can browse the types and their members exposed by a type library using the VBE's Object Browser (F2). 您可以使用VBE的对象浏览器 (F2) 浏览类型库公开的类型及其成员。 If there's no
Excel
library and no Application
class other than the one define in the Access
library, you're missing a reference. 如果没有
Excel
库,但是没有Application
比一个其他类中定义Access
库,你缺少的参考。
Then... 然后...
Make the function explicitly Public
, the array parameters explicitly ByRef
, the non-array parameters explicitly ByVal
, give them all an explicit declared type. 将函数显式设置为
Public
,将数组参数显式设置为ByRef
,将非数组参数显式设置为ByVal
,为它们提供所有显式声明的类型。
Public Function GetAIRR(ByRef arrAssCF As Variant, ByRef arrAssDate As Variant, ByVal Guess As Long) As Double
Guess
could probably of type xlGuess
, but that's ...well, a guess . Guess
的类型可能是xlGuess
,但这是……嗯,一个猜想 。 Long
should be good enough, but if an enum exists that could make it easier for the calling code to know what to pass for that parameter, use it. Long
应该足够好,但是如果存在一个枚举,可以使调用代码更容易知道对该参数传递什么,请使用它。
You don't need a local Excel.Application
variable - just have a With
block hold that reference for you: 您不需要本地
Excel.Application
变量-只需使用With
块为您保存该引用即可:
With New Excel.Application
Target = .Xirr(arrAssCF, arrAssDate, Guess)
End With
Lastly, the Target
variable doesn't seem to be declared anywhere, and the function isn't returning anything. 最后,似乎未在任何地方声明
Target
变量,并且该函数未返回任何内容。 Did you mean to do this? 您是要这样做吗?
With New Excel.Application
GetAIRR = .Xirr(arrAssCF, arrAssDate, Guess)
End With
Note that invoking the Xirr
function off Excel.Application
is a late-bound call that will be resolved at run-time, and if something goes wrong with it, you'll get a type mismatch error when GetAIRR
(a Double
) is tentatively assigned to some Error
value. 请注意,从
Excel.Application
调用Xirr
函数是一个后期绑定调用,它将在运行时解决,如果出现问题,则在临时分配GetAIRR
( Double
)时会收到类型不匹配错误。到一些Error
值。
By using Excel.WorksheetFunction.Xirr
instead, the call is early-bound (ie resolved at compile-time), and if something goes wrong with it, the function will raise an actual run-time error instead of returning one; 通过使用
Excel.WorksheetFunction.Xirr
,该调用是早期绑定的(即在编译时解决),并且如果出现问题,该函数将引发实际的运行时错误,而不是返回一个错误。 you can handle that error with normal error handling, or you can suppress either error and return 0
, if that's an acceptable thing for this function to do: 您可以使用常规错误处理来处理该错误,或者可以抑制任何一个错误并返回
0
,如果此函数可以接受的话:
With New Excel.Application
On Error Resume Next
'possible type mismatch if input is invalid
GetAIRR = .Xirr(arrAssCF, arrAssDate, Guess)
'or early-bound:
'possible error 1004 if input is invalid
'GetAIRR = .WorksheetFunction.Xirr(arrAssCF, arrAssDate, Guess)
On Error GoTo 0
End With
.Xirr
is a WorksheetFunction. .Xirr
是一个WorksheetFunction。 Use the following: 使用以下内容:
Application.WorksheetFunction.Xirr(arrAssCF, arrAssDate, Guess)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.