VBA Exel 编译错误:预期:由“(”引起的语句结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43647372/
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
VBA Exel Compile error: Expected: end of statement caused by " ("
提问by Alice Galvin
I am trying to create a custom formula for the following:
我正在尝试为以下内容创建自定义公式:
to take Name, Affiliation, and Email in separate cells and combine them in a new cell as Name (Affiliation) .
将 Name、Affiliation 和 Email 在单独的单元格中,并将它们组合在一个新单元格中作为 Name (Affiliation) 。
I am able to do this straight into a new cell using the formula
我可以使用公式将其直接执行到新单元格中
=A2& " ("& B2&") " & "<"&C2&">"
where A2, B2, and C2 are the cells containing Name, Affiliation, and Email respectively.
其中 A2、B2 和 C2 分别是包含姓名、隶属关系和电子邮件的单元格。
But instead of copy-and-pasting this formula every time, I am trying to create a custom formula as a short cut. However, when I try and use this same formula in VBA to create a new module like this:
但是我不是每次都复制粘贴这个公式,而是尝试创建一个自定义公式作为捷径。但是,当我尝试在 VBA 中使用相同的公式来创建这样的新模块时:
Function Combined (name, affiliation, email)
Combined = name&" ("&affiliation&") "&"<"&email&">"
the program highlights the " (" and it gives me the following error:
该程序突出显示“(”,它给了我以下错误:
Compile error: Expected: end of statement
编译错误:预期:语句结束
I am not a coder, I am just dabbling, but any help would be greatly appreciated.
我不是编码员,我只是涉足,但任何帮助将不胜感激。
Thank you!
谢谢!
P.S. If I could do a range like A2:C2 rather than A2,B2,C2, for the same effect, that would be even better!!!!!!
PS如果我能做一个像A2:C2而不是A2,B2,C2这样的范围,以获得相同的效果,那就更好了!!!!!!
回答by Mathieu Guindon
Short version
精简版
Add spaces between operators and their operands. Problem solved.
在运算符及其操作数之间添加空格。问题解决了。
Long version
长版
Any identifier that is immediately followed by a &, like name&and affiliation&, is interpreted as a Longvariable, so the lack of whitespace in front of what's meant to be concatenation operators is causing a parse error, because VBA doesn't know what literal expression could possibly follow the Combined = name&assignment - the instruction is complete as it is; the only token that shouldbe where " ("is, is an end-of-statementtoken:
任何紧跟 a &、 like name&and 的标识符都affiliation&被解释为一个Long变量,因此在表示连接运算符之前缺少空格会导致解析错误,因为 VBA 不知道可能跟随什么文字表达式在Combined = name&分配-指令的完成,因为它是; 唯一应该在的标记" ("是语句结束标记:
Expected: end of statement
预期:语句结束
Says exactly that. Everything before " ("is a perfectly valid instruction, except it's not terminated.
正是这样说的。之前的所有" ("指令都是完全有效的指令,除非它没有终止。
So it's not the " (", it's the type hints. Insert spaces to separate the operators from the operands, and you'll fix the problem. More explicitness couldn't hurt, either:
所以这不是" (",而是类型提示。插入空格将运算符与操作数分开,您将解决问题。更明确也不会伤害:
Option Explicit
Public Function Combined(ByVal name As String, ByVal affiliation As String, email As String) As String
Combined = name & " (" & affiliation & ") " & "<" & email & ">"
End Function
When a type isn't specified, a declaration is implicitly Variant, which incurs some unnecessary run-time overhead.
当未指定类型时,声明是隐式的Variant,这会导致一些不必要的运行时开销。
When ByValisn't specified, parameters are passed ByRefby default, which means the function could be assigning to the parameters.
当ByVal未指定时,ByRef默认传递参数,这意味着函数可以分配给参数。
You could also implement the function like so:
您还可以像这样实现该功能:
Combined = Join(Array(name, "(" & affiliation & ")", "<" & email & ">"), " ")
回答by YowE3K
If you want to pass a Rangeto your function you could do:
如果你想将 a 传递Range给你的函数,你可以这样做:
Public Function Combined(rng As Range) As Variant
If rng.Cells.Count <> 3 Or rng.Areas.Count <> 1 Then
Combined = cvErr(xlErrValue)
Else
Combined = rng.Cells(1).Value & _
" (" & rng.Cells(2).Value & ")" & _
" <" & rng.Cells(3).Value & ">"
End If
End Function
If placed in a standard code module, that function can then be invoked from Excel as, for instance, =Combined(A2:C2)
如果放置在标准代码模块中,则可以从 Excel 调用该函数,例如, =Combined(A2:C2)

