简体   繁体   English

编译错误:未定义用户定义的类型Access / Excel参考

[英]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函数是一个后期绑定调用,它将在运行时解决,如果出现问题,则在临时分配GetAIRRDouble )时会收到类型不匹配错误。到一些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.

相关问题 Excel VBA 编译抛出“未定义用户定义的类型”错误 - Excel VBA Compile throws a “User-defined type not defined” error 后期绑定编译错误:未定义引用Excel VBA中的Outlook mailitem的用户定义类型 - Late binding compile error: User-defined type not defined referencing Outlook mailitem in Excel VBA 如何修复编译错误:在 Outlook 中使用 Excel VBA 时未定义用户定义的类型? - How to fix Compile Error: User-defined type not defined when using Excel VBA from Outlook? 编译错误用户定义类型未在“oShell As WshShell”中定义 - compile error User-defined type not defined at “oShell As WshShell” 昏暗的字典。 编译错误:未定义用户定义的类型 - Dim As Dictionary. Compile Error: User-defined type not defined 编译错误:更新 Access 库的旧宏时未定义用户定义的类型 - Compile error: User-defined type not defined when updating an older macro for Access library Outlook 错误:未定义用户定义类型引用 Excel 工作簿 - Outlook Error: user-defined type not defined referring to Excel workbook 在 Access VBA 中将变量声明为 Excel 应用程序生成错误:未定义用户定义的类型 - Declaring variable as Excel application in Access VBA generates error: User-defined type not defined 用户定义类型未定义错误 - User-defined type not defined error Excel VBA On用户定义类型的错误处理 - Excel VBA On Error handling with User-Defined Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM