Concatenating strings in VBA

前端 未结 3 696
心在旅途
心在旅途 2021-01-17 18:29

I\'m maintaining an application written in Microsoft Access with VBA.

I\'m glancing over my code and have just noticed I have subconsciously been concatenating strin

3条回答
  •  粉色の甜心
    2021-01-17 19:02

    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.

提交回复
热议问题