excel - 在VBA Excel中调用sub中的函数时出现"Object Required"

标签 excel function vba object required

在我的 Excel VBA 程序中;我有一个名为“ParamCheck”的函数,它获取四个“ double ”变量,检查它们并以“字符串”形式返回消息。

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”

最佳答案

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

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

关键字Set用于分配对对象(工作表、范围等)的引用。

如果您尝试将引用分配给数据类型,就像您所做的那样,您将收到错误。

关于excel - 在VBA Excel中调用sub中的函数时出现"Object Required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16413159/

相关文章:

excel - 如何使用 XML HTTP 请求在 Visual Basic for Applications 中提取 Web 数据?

vba - 将单行复制到另一个工作表 VBA EXCEL

excel - 如何同步多个Excel输入单元格

sql - 编写多个条件时的 Case 的更好方法

function - powershell 参数需要在脚本的前面吗?

c - 对 'WinMain' 的 undefined reference [错误] ld 返回 1 退出状态,将矩阵作为函数中的参数传递

c++ - 无法从另一个对象调用成员函数

VBA 加载项 : how to "embed" the required reference libraries? 向其他用户发送功能加载项时获取 "Compile error in hidden module"

vba - 在 VBA 中按相同大小分组

java - 如何使用 Apache POI 从 excel (xlsx) 文件读取整数单元格值作为字符串而不进行任何更改