调用VBA Excel中的子函数时,“Object Required”

在我的Excel的VBA程序; 我有一个名为“ParamCheck”的函数,它会得到四个“double”variables,检查它们并将消息返回为“string”。

Function ParamCheck(airSpeed As Double, FCUvalue As Double, _ altitude As Double, terrainElevation As Double) As String If airSpeed < MIN_CONTROL_SPEED Then 'check if airspeed is less than 119 ft/min or not ParamCheck = "Airspeed is less than minimum control speed" ElseIf FCUvalue > FCU_VALUE_LIMIT Then 'check if FCU value if greater than 10 or not ParamCheck = "FCU value is greater than limit" ElseIf FCUvalue < 0 Then 'check if FCU vlaue is negative or not ParamCheck = "FCU value is negative" ElseIf altitude <= terrainElevation Then 'check if altitude is greater that terrain of elevation or not ParamCheck = "Altitude is less than terrain elevation" Else 'if all the parameters are valid print a "Valid" message ParamCheck = PARAMS_OK End If End Function 

现在我需要在我的子程序中调用这个函数。 这是代码

 Dim checkParam As String ' result of validity check of parameters Set checkParam = ParamCheck(speedAir, valueFCU, aboveSea, elevationTerrain) 

当运行它给我这个错误“所需的对象”,并突出显示“checkParam”

你不需要Stringtypes的关键字Set ,这就是为什么。

与其他编程语言不同,VBA将String视为数据types ,而不是Object

关键字Set用于指定一个对象(工作表,范围等)的引用。

如果您尝试为数据types分配一个引用,那么您将会收到一个错误。