VBA 获取返回字符串的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14427609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:15:30  来源:igfitidea点击:

VBA Getting a function to return a string

vba

提问by Edgar Aroutiounian

I have a column of values for universities. For example, I have a column with string "University of Michigan" and "UMich"

我有一列大学的价值观。例如,我有一列字符串“密歇根大学”和“UMich”

why won't the following function return back in the cell a string "University of Michigan"

为什么以下函数不会在单元格中返回字符串“密歇根大学”

Function CleanUniCode(entry) As Variant
If entry = "UMich" Then entry = "University of Michigan"

End Function

Also tried this and the cell is returning 0, not sure why.

也试过这个,单元格返回 0,不知道为什么。

Function CleanUniCode(entry) As Variant
If entry Like "[UMich]" Then
entry = "University of Michigan"
ElseIf entry Like "[UPenn]" Then
entry = "University of Pennsylvania"
Else:
End If

End Function

回答by Anton

It's because you assigned the return value to entry, but you should assign it to CleanUniCode:

这是因为您将返回值分配给entry,但您应该将其分配给CleanUniCode

Function CleanUniCode(entry) As Variant
    If entry = "UMich" Then CleanUniCode = "University of Michigan"
End Function

Always assign the return value to the function name.

始终将返回值分配给函数名称。