如何在 VBA 中连接字符串? [英] How can I concatenate strings in VBA?

查看:73
本文介绍了如何在 VBA 中连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This question comes from a comment under Range.Formula= in VBA throws a strange error.

我通过反复试验编写了那个程序,所以我很自然地尝试了 + 来连接字符串.

I wrote that program by trial-and-error so I naturally tried + to concatenate strings.

但是 & 是否比 + 更适合连接字符串?

But is & more correct than + 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

这只是潜在错误的一个微妙来源,因此应该避免.& always 表示字符串连接",即使它的参数是非字符串:

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"

这篇关于如何在 VBA 中连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆