vba 需要从vba中的函数返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45010499/
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
Need to return value from a function in vba
提问by nishit dey
I am trying to return a value from a function but is not happening.
我试图从函数返回一个值,但没有发生。
Public sub test()
x = 2
msgbox number(x)
Exit Sub
Public Function number(num)
if len(num) = 1 then
num = "0" + num
End if
End Function
The function is returning a null value.
该函数返回空值。
回答by ainwood
Public function number(num as string) as string
If len(num) = 1 then
Number ="0" & num
Else
Number = num
End if
End function
You need to have code in the function that sets a value to the name of the function - the return value.
您需要在函数中使用代码将值设置为函数的名称 - 返回值。
回答by Rory
You need to assign the result to the name of the function, and concatenate the 0, like this:
您需要将结果分配给函数的名称,并连接 0,如下所示:
Public Function number(num) As String
if len(num) = 1 then
number = "0" & num
Else
number = num
End if
End Function
though it would seem easier to just use Format(num, "00")
虽然看起来更容易使用 Format(num, "00")