0

I have a basic VBA question. See my code below, I would expect an error message for the first msgbox because you can't calculate an integer plus a string, but that's not the case. What is happening? Why, VBA seems to be changing data type here? Thanks in advance.

Sub calc1()
  Dim x As String
  x = 100
  MsgBox 1 + x ' display 101
  Msgbox "1" + x ' display 1100
End Sub
Community
  • 1
  • 1
  • 1
    `+` is two things, depending on context ("addition" operator, or "concatenation" operator). If you want to *concatenate strings*, use the `&` operator instead. Yes, VBA is doing implicit conversions behind your back. All. The. Time. But JavaScript is even worse. – Mathieu Guindon Jan 18 '17 at 18:20
  • Related: http://stackoverflow.com/questions/11595226/how-do-i-convert-an-integer-to-a-string-in-excel-vba – Tim Williams Jan 18 '17 at 18:20

0 Answers0