从VBA函数返回一个字符串

时间:2016-12-14 11:25:05

标签: vba

我正在关注教程并在hello world示例函数中获得编译错误。

这里有什么问题?

enter image description here

这是我尝试过的代码:

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

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

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

1 个答案:

答案 0 :(得分:0)

您可以使用两种方法来实现“Hello Worls示例。

选项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 returnedstring result 
TstHiFunc = Hi("Hello World")

' for debug only
MsgBox TstHiFunc

End Sub
相关问题