VBA Exel编译错误:预期:由“;"引起的语句结尾(“ [英] VBA Exel Compile error: Expected: end of statement caused by " ("
问题描述
我正在尝试为以下各项创建自定义公式:
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) .
我可以使用公式
=A2& " ("& B2&") " & "<"&C2&">"
其中A2,B2和C2分别是包含名称",从属关系"和电子邮件"的单元格.
where A2, B2, and C2 are the cells containing Name, Affiliation, and Email respectively.
但是,我不是每次都复制并粘贴此公式,而是尝试创建一个自定义公式作为快捷方式.但是,当我尝试在VBA中使用相同的公式来创建像这样的新模块时:
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:
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.
谢谢!
P.S.如果我可以像A2:C2这样的范围而不是A2,B2,C2做同样的效果,那就更好了!
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!!!!!!
推荐答案
简短版本
在运算符及其操作数之间添加空格.问题解决了.
Add spaces between operators and their operands. Problem solved.
长版
任何紧跟着&
的标识符(例如 name&
和 affiliation&
)都被解释为 Long
变量,因此在表示串联运算符之前缺少空格会导致解析错误,因为VBA不知道什么文字表达式可能遵循 Combined = name&
作业-指令按原样完成;应该应位于("
"所在的位置)的唯一令牌是声明终止令牌:
Any identifier that is immediately followed by a &
, like name&
and affiliation&
, is interpreted as a Long
variable, 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 should be where " ("
is, is an end-of-statement token:
预期:语句结束
完全是这样.("("
)之前的所有内容都是完全有效的指令,除非它没有终止.
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
如果未指定类型,则声明是隐式的 Variant
,这会导致不必要的运行时开销.
When a type isn't specified, a declaration is implicitly Variant
, which incurs some unnecessary run-time overhead.
当未指定 ByVal
时,默认情况下会通过 ByRef
传递参数,这意味着该函数可以分配给这些参数.
When ByVal
isn't specified, parameters are passed ByRef
by default, which means the function could be assigning to the parameters.
您也可以像这样实现该功能:
You could also implement the function like so:
Combined = Join(Array(name, "(" & affiliation & ")", "<" & email & ">"), " ")
这篇关于VBA Exel编译错误:预期:由“;"引起的语句结尾(“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!