在 VBA 中连接字符串 [英] Concatenating strings in VBA
问题描述
我正在使用 VBA 维护一个用 Microsoft Access 编写的应用程序.
I'm maintaining an application written in Microsoft Access with VBA.
我正在浏览我的代码,刚刚注意到我已经下意识地将字符串与加号 (+) 符号而不是 & 符号连接在一起.自从我用 VB6 编码以来已经有几年了.这会导致任何问题吗?
I'm glancing over my code and have just noticed I have subconsciously been concatenating strings together with the plus (+) symbol instead of the ampersand. It's been a few years since I've coded in VB6. Could this cause any issues?
一切似乎都很好,只需几分钟即可修复,我只是好奇我是否在技术上做错了什么.
Everything seems fine and it will only take a few minutes to fix, I'm just curious as to whether I'm technically doing anything wrong.
推荐答案
& 是明确的字符串操作,而 plus 是重载的:
The ampersand is explicitly a string operation, while the plus is overloaded:
Dim num1 As Integer
num1 = RandomNumberBetween(1, 9)
Dim num2 As Integer
num2 = RandomNumberBetween(1, 9)
Dim randomAge As String 'trying to get a random age between 11 and 99
' works
randomDate = "Your age is " & num1 & num2
'broken
randomDate = "Your age is " + num1 + num2
当与数字一起使用时,加号将添加.
When used with numbers the plus sign will add.
这篇关于在 VBA 中连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!