How Do I Convert an Integer to a String in Excel VBA? How Do I Convert an Integer to a String in Excel VBA? vba vba

How Do I Convert an Integer to a String in Excel VBA?


CStr(45) is all you need (the Convert String function)


Try the CStr() function

Dim myVal as String;Dim myNum as Integer;myVal = "My number is:"myVal = myVal & CStr(myNum);


Most times, you won't need to "convert"; VBA will do safe implicit type conversion for you, without the use of converters like CStr.

The below code works without any issues, because the variable is of Type String, and implicit type conversion is done for you automatically!

Dim myVal As StringDim myNum As IntegermyVal = "My number is: "myVal = myVal & myNum

Result:

"My number is: 0"

You don't even have to get that fancy, this works too:

Dim myString as StringmyString = 77

"77"

The only time you WILL need to convert is when the variable Type is ambiguous (e.g., Type Variant, or a Cell's Value (which is Variant)).

Even then, you won't have to use CStr function if you're compounding with another String variable or constant. Like this:

Sheet1.Range("A1").Value = "My favorite number is " & 7

"My favorite number is 7"

So, really, the only rare case is when you really want to store an integer value, into a variant or Cell value, when not also compounding with another string (which is a pretty rare side case, I might add):

Dim i as Integeri = 7Sheet1.Range("A1").Value = i

7

Dim i as Integeri = 7Sheet1.Range("A1").Value = CStr(i)

"7"