VBA 从被调用函数调用函数时 byref 参数类型不匹配

VBA byref argument type mismatch when calling a function from a function being called

希望有人能提供帮助。这是我正在使用的一组代码的简化版。我正在调用一个函数,而那个函数又调用了另一个函数。变量从调用子传递给第一个被调用函数,从那里它应该传递给第二个被调用函数到 return 一个值。但是,我在第一个函数的 "Parameter" 下方收到 ​​"byref argument type mismatch" 错误。有什么建议么?谢谢!

' Sub to call function 1
Sub TestFunctionSelect()
    Dim X As Double
    ' X should = the value of the function mShareMMTDaily as called from mFunctionSelect
    X = mFunctionSelect("mShareMMTDaily", "SOL", "2008/02/28", 12)
End Sub

' Function 1 to call function 2 and return a value to the sub
Function mFunctionSelect(FunctionName As String, CompCode As String, CurrentMonth As Date, Parameter As Double) As Double
    Select Case FunctionName
        ' Case Is = "mValue"
            ' mFunctionSelect = mValue(CompCode, CurrentMonth, Parameter)
        Case Is = "mShareMMTDaily"
            ' This function is called
            ' I get the "byref argument type mismatch" error on the below "Parameter"
            mFunctionSelect = mShareMMTDaily(CompCode, CurrentMonth, Parameter)
    End Select
End Function

Function mShareMMTDaily(Code As String, ShareDate As Date, LookBack As Integer) As Double
    ' Do Stuff
End Function

您的参数变量被声明为 Double,并且您将其传递给需要 Integer 的函数。您应该转换它,如:

mFunctionSelect = mShareMMTDaily(CompCode, CurrentMonth, CInt(Parameter))

顺便说一句,正如@dee 指出的那样 - 您还对字符串进行了隐式转换。这会使您的代码不那么安全,并且依赖于运行代码的主机的语言设置。您应该明确转换日期,或者更好的是,从一开始就使用日期而不是字符串。