2011-06-07 174 views
9

我在VBA中有简单的功能,我需要检查它是否已经成功执行。我不太了解VBA,所以我不知道它是否可行。我想要做这样的事情:bool X=MyFunction()函数返回布尔值?

我在QTP描述性编程中使用VBA。这不起作用:

Function A as Boolean 
    A=true 
End Function 

它说:Expected statement

,但我看不到任何返回类型在我的方法等

回答

18
function MyFunction() as Boolean 
    ..... 
    ..... 
    MyFunction = True 'worked 
end function 

dim a as boolean = MyFunction() 
6

在VBA中,您可以通过分配设置函数的返回值的变量具有相同名称的功能:

Function MyFunc() as Boolean 
    MyFunc = True 
End Function 
+0

谢谢,我没有写它的VBA QTP内。当我尝试这个时,它说“期望的声明”。 – Mirial 2011-06-07 12:22:22

+0

这是什么QTP? – codeape 2011-06-07 12:25:03

+0

惠普Quick Test Professional。 – Mirial 2011-06-07 12:26:37

0

那么如果你有机会ŧ o你的函数声明,你可以为它设置一个bool返回类型,并根据执行返回true或false。 一旦你的函数返回一个布尔值,你的代码就可以工作。

0

没有真正的方法来检查函数是否在VBA中工作。你必须自己决定你的功能是否成功。例如:

Function AFunction() as Boolean 
    on error goto 1 
    MyFunc = True 
    AFunction = True 
    Exit Function 
    1 
    AFunction = False 
End Function 

上面的代码会让你知道函数是否失败。如果失败,则转到标签“1”,然后返回false,否则返回true。

如果它不是您正在查找的“错误”,那么您必须确定返回或提供的数据是否正确。这样做的一种方法是返回一个代表失败的特定值[error-code]。

3

我怀疑你可能使用VBScript而不是VBA?如果是这样的话,那么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 

或工作在你的榜样

Function A()  
A=true 
End Function