我在VBA中有一个简单的函数,我需要检查它是否成功执行。我不太了解VBA,所以不知道是否可能。我想做这样的事情:bool X=MyFunction()
。
我在QTP描述性编程中使用VBA。这个方法不起作用:
Function A as Boolean
A=true
End Function
它说: 预期语句
但是我在我的方法中看不到任何返回类型等。
function MyFunction() as Boolean
.....
.....
MyFunction = True 'worked
end function
dim a as boolean = MyFunction()
Function MyFunc() as Boolean
MyFunc = True
End Function
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
Function A()
A=true
End Function
在VBA中,没有真正的方法来检查函数是否有效。您必须自己决定函数是否成功。例如:
Function AFunction() as Boolean
on error goto 1
MyFunc = True
AFunction = True
Exit Function
1
AFunction = False
End Function
上述内容将告诉你函数是否失败。如果它失败了,那么它会转到标签“1”,然后返回false;否则,它将返回true。
如果你不是在寻找“错误”,那么你必须决定返回或提供的数据是否正确。一种方法是返回一个特定值[错误码],表示失败。