如何连接VBA中的字符串

问题描述:

这个问题来自于 Range.Formula = VBA的评论抛出一个奇怪的错误

我通过反复错误的方法写了该程序,所以我自然尝试了 + 连接字符串。
但是& 而不是 + 连接字符串的更正确的方法?

I wrote that program by trial-and-error method so I naturally tried + to concatenate strings. But is & rather than + more correct method for concatenating strings?

& 在字符串上下文中始终如果其中一个操作数不是字符串, + 可能不会连接:

& is always evaluated in a string context, while + may not concatenate if one of the operands is no string:

"1" + "2" => "12"
"1" + 2   => 3
1 + "2"   => 3
"a" + 2   => type mismatch

这只是一个潜在错误的微妙来源,因此应该避免。总是意味着字符串连接,即使它的参数是非字符串:

This is simply a subtle source of potential bugs and therefore should be avoided. & always means "string concatenation", even if its arguments are non-strings:

"1" & "2" => "12"
"1" &  2  => "12"
 1  & "2" => "12"
 1  &  2  => "12"
"a" &  2  => "a2"