在VBA Excel中调用sub中的函数时“Object Required”

时间:2013-05-07 07:06:34

标签: excel function vba object required

在我的VBA程序中为excel;我有一个名为“ParamCheck”的函数,它获取四个“双”变量,检查它们并返回一个消息“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”

1 个答案:

答案 0 :(得分:8)

对于String类型,您不需要关键字Set,这就是原因。

与其他编程语言不同,VBA将字符串视为数据类型,而不是对象

关键字Set用于指定对象的引用(Worksheet,Range等)。

如果您尝试为数据类型分配引用,则会出现错误。