vba - 从 VBA 函数返回一个字符串

标签 vba

我正在学习教程并在 hello world 示例函数中遇到编译错误。

这里有什么问题吗?

enter image description here

这是我试过的代码:

Function hi()
    hi = "hello world"
End Function`

编辑:建议的声明没有帮助 enter image description here

编辑:越来越近了。调用“hi()”时括号似乎是个问题 enter image description here

最佳答案

您可以使用 2 种方法来实现您的“Hello World”示例。

选项 1: 使用常规 Sub 简单且足以满足您的示例:

Sub Hi_()

Dim HiStr   As String

HiStr = "Hello World"
MsgBox HiStr

End Sub

选项 2:Function 与“Hello World”示例结合使用:

Function Hi(TestHi As String) As String

' Input: this function receives a string as a parameter
' Output: returns a string

Hi = "Test Function with " & TestHi

End Function

现在我们需要一个 Sub 来测试 Function:

Sub Test_Hi_Function()

Dim TstHiFunc As String

' send "Hello World" to Function Hi as a parameter
' TstHiFunc gets the returned string result 
TstHiFunc = Hi("Hello World")

' for debug only
MsgBox TstHiFunc

End Sub

关于vba - 从 VBA 函数返回一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141388/

相关文章:

vba - 创建和评分测验的建议

sql - Excel 对象的语法。更改数字格式

Excel VBA : Sheet protection not working as desired

excel - 如何通过 Excel 网络查询从 Google Directions API 中提取距离?

vba - 我可以使用 VBA 将网络图像 (gif) 导入到 Excel 中吗?

VBA每天在Excel中的数据范围之间写入

excel - VBA Class() 对象作为另一个类的属性

Excel 基于颜色条件格式化数据条

Excel:计算自动过滤表中单元格和单元格之间的差异

vba - 向 Range 中的特定单元格添加注释。 Excel VBA