excel - 将 VBA 变量设置为字符串函数的结果

标签 excel vba function

我今天读了很多关于函数的文章,它们似乎都在处理数学/数字。我正在尝试使用一个返回字符串并将其捕获为“主子”中的变量的函数,但我无法让它工作。谁能指出我做错了什么?

例如:

功能:

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/

相关文章:

vba - 通过UDF获取Google搜索第一个结果

excel - 如何使用 VBA 在 Internet Explorer 中单击按钮

c - 不知道如何使用/初始化结构函数

vba - 使用 Sheets.Range.Offset 的行上的应用程序或对象定义的错误

python - 使用 openpyxl。从列中制作列表

excel - 从excel中较大的列填充数据库列

bash - 函数与脚本 - Unix Bash 编程

excel - 具有不同值的不同变量的行的总和

android - 是否可以在Android Studio项目中运行宏?

function - 将属性传递给函数管道输出错误