vba 函数返回布尔值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6265024/
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
Function Returning Boolean?
提问by Mirial
I have simple function in VBA and I would need to check whether or not it has been successfully performed. I do not know VBA much, so I have no idea whether or not its possible. I want to do something like this: bool X=MyFunction()
.
我在 VBA 中有简单的功能,我需要检查它是否已成功执行。我不太了解 VBA,所以我不知道它是否可能。我想要做这样的事情:bool X=MyFunction()
。
I am using VBA in the QTPdescriptive programming. This does not work:
我在QTP描述性编程中使用 VBA 。这不起作用:
Function A as Boolean
A=true
End Function
It says: Expected statement
它说: Expected statement
But I cannot see any return type in my method etc.
但是我在我的方法等中看不到任何返回类型。
回答by Rene de la garza
function MyFunction() as Boolean
.....
.....
MyFunction = True 'worked
end function
dim a as boolean = MyFunction()
回答by codeape
In VBA, you set a function's return value by assign to a variable with the same name as the function:
在 VBA 中,您可以通过分配给与函数同名的变量来设置函数的返回值:
Function MyFunc() as Boolean
MyFunc = True
End Function
回答by osknows
I suspect you may be using VBScript instead of VBA? If that's the case then VBScript doesn't state Type
我怀疑您可能正在使用 VBScript 而不是 VBA?如果是这种情况,则 VBScript 不会说明 Type
this will work in VBScript
这将在 VBScript 中工作
dim test,b
test = 1
b=false
msgbox ("Calling proc before function test=" & test)
msgbox("Calling proc before function b=" & b)
b = A(test)
msgbox ("Calling proc after function test=" & test)
msgbox("Calling proc after function b=" & b)
Function A(test)
test = test +1
A=true
End Function
or in your example
或者在你的例子中
Function A()
A=true
End Function
回答by Serodis
There is no real way to check if a function worked in VBA. You must decide for yourself whether your function was successful. For example:
没有真正的方法可以检查函数是否在 VBA 中工作。您必须自己决定您的功能是否成功。例如:
Function AFunction() as Boolean
on error goto 1
MyFunc = True
AFunction = True
Exit Function
1
AFunction = False
End Function
The above would let you know if the function failed. If it fails, it goes to the label '1' and then returns false, otherwise, it returns true.
如果函数失败,上面会告诉你。如果失败,则转到标签“1”,然后返回 false,否则返回 true。
If it isn't an 'error' you are looking for, then you must decide if the data returned or supplied is proper. One way of doing this is to return a specific value [error-code] which represents a failure.
如果它不是您要查找的“错误”,那么您必须确定返回或提供的数据是否正确。这样做的一种方法是返回一个表示失败的特定值 [error-code]。
回答by chamel
Well if you have access to your function declaration, you can set a bool return type for it and return true or false depending on the execution. Once your function returns a bool, your code will work.
好吧,如果您可以访问您的函数声明,您可以为它设置一个 bool 返回类型,并根据执行情况返回 true 或 false。一旦您的函数返回一个布尔值,您的代码就会起作用。