在 VBA Excel 中调用 sub 中的函数时“需要对象”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16413159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Object Required" when calling function in sub in VBA Excel
提问by user1143529
In my VBA program for excel; I have a function called "ParamCheck" which get four "double" variables, checks them and returns a message as "string".
在我的 excel VBA 程序中;我有一个名为“ParamCheck”的函数,它获取四个“双”变量,检查它们并以“字符串”形式返回一条消息。
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
and now I need to call this function in my sub program. Here is the code
现在我需要在我的子程序中调用这个函数。这是代码
Dim checkParam As String ' result of validity check of parameters
Set checkParam = ParamCheck(speedAir, valueFCU, aboveSea, elevationTerrain)
when running it gives me this error "object required" and highlights "checkParam"
运行时它给了我这个错误“需要对象”并突出显示“checkParam”
回答by Adrien Lacroix
You don't need the keyword Set
for the type String, that's why.
您不需要Set
String 类型的关键字,这就是原因。
Unlike to other programming languages, VBA considers Stringas a Data Type, and not as an Object.
与其他编程语言不同,VBA 将String视为Data Type,而不是Object。
The keyword Set
is used to assign a reference to an object (Worksheet, Range, etc...).
关键字Set
用于分配对对象(工作表、范围等)的引用。
If you try to assign a reference to a data type, as you did, you will get an error.
如果您尝试将引用分配给数据类型,就像您所做的那样,您将收到错误消息。