gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:45:36 30 4
gpt4 key购买 nike

我正在学习教程并在 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com