我今天读了很多关于函数的文章,它们似乎都在处理数学/数字。我正在尝试使用一个返回字符串并将其捕获为“主子”中的变量的函数,但我无法让它工作。谁能指出我做错了什么?
例如:
功能:
Public Function Test(var) As Variant
Bar = var & "World"
MsgBox Bar
End Function
子:
Public Bar As Variant
Public Z As Variant
Sub testing()
Call Test("Hello") ' This displays "HelloWorld" from MsgBox in Function
Test ("Hello") ' This displays "HelloWorld" from MsgBox in Function
Z = Test("Hello") ' This displays "HelloWorld" from MsgBox in Function
MsgBox Z ' This displays an empty MsgBox :*(
End Sub
最佳答案
如果您希望函数返回一个值,请填充函数变量并将其返回到主子中,如下所示
Public Function Test(var As String) As String
Test = var & " World"
End Function
Sub testing()
returnstr = Test("Hello")
MsgBox returnstr
End Sub
关于excel - 将 VBA 变量设置为字符串函数的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55585923/