Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Viveka Fischer   on Feb 21 In MS Office Category.

  
Question Answered By: Benny Torres   on Feb 21

OK, so to clarify, both + and & can be used to perform string  concatenation.
& is preferred, because it specifically applies to strings, whereas + can be
used for both numbers and strings, which can lead to code ambiguity.

If you use &, you must put a space in front of the & when you type it. The
VBA editor will automatically reformat it so it has spaces on both sides.
You can type the + with no spaces around it, and the editor will put them
both in.

To show an example of the ambiguity, the following two functions produce
different results:

Sub s()
Dim a As String
Dim b as Integer
Dim c As Integer
a = "5"
b = "6"
c = a + b
Debug.Print c
End Sub

Sub s2()
Dim a As String
Dim b as String ' b is now a String also
Dim c As Integer
a = "5"
b = "6"
c = a + b
Debug.Print c
End Sub

whereas, using & always yields "56".

Share: 

 

This Question has 10 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on how to concatenate string variables Or get search suggestion and latest updates.


Tagged: