PDA

View Full Version : Convert Integer to String



WizHock
07-31-2008, 07:35 AM
Silly Question:

I know in Visual Studio there is the .ToString() function which allows the conversion of a non-string data type to be converted to a String. What is the equivalent, if any, in VBA? I've searched the Help a little, but don't really have the time to do the leg work on that on.

Thanks in advance.

Dave
07-31-2008, 08:31 AM
HTH. Dave

Dim Num As Double, Str As String
Num = 1000.34
Str = Num
MsgBox Str

CreganTur
07-31-2008, 09:25 AM
You can also use Type Conversion Functions, which are built into VBA to change the data type of anything.

Search the VBA Help File for "Type Conversion Functions" for the whole list.

For your example:

Dim i As Long
i = 100
Left (CStr(i),1)
The above takes a Long type number (i) and converts it into a string just for the Left function.

macropod
07-31-2008, 02:14 PM
Hi WizHock,

Dave's code doesn't convert the integer to a string at all - it just assigns it to a different variable.

Randy's implementation of CStr is the correct way to do the conversion, but his code only returns the left-most character.

Try:
Sub Test()
Dim i As Long
Dim Str as String
i = 100
Str = CStr(i)
MsgBox Str
End Sub

Dave
07-31-2008, 05:53 PM
Hi macropod. Apparently I could use a bit more learning on this topic. I don't get the difference. Changing the variable from a long to a string does "convert" the contents of the variable into a string format. For instance, when entering numeric info to a Word doc from XL, the numeric variable (ie. long) has to be "converted" (aka for me..."changed") to a string before it can be successfully transferred. Perhaps I need a bit more info about what is meant by conversion if you would care to donate a bit more time. Dave

WizHock
07-31-2008, 08:20 PM
Yea CStr() does the job. I knew there was something similar to .ToString() but I've only briefly, six years ago, worked with VB 6 so I couldn't remember.

Thanks!!

macropod
07-31-2008, 09:38 PM
Hi Dave,

What you've done in your code is to assign a double precsion number to a string. Although you can use it that way, a true 'conversion' hasn't been done: hence the existence of vba's 'Type Conversion Functions'. Some of these functions also operate rather differently than just assigning data of one type to another. You're also relying on an unsupported 'feature' - should a future release of vba (or its ultimate replacement) operate differently, your code may fail. There's a fair bit of useful infor on this in the vba help and look up the individual 'return type' entries there.

CreganTur
08-01-2008, 05:16 AM
Dave,

Another facet of the issue is the fact that your version is 'cluttering' your code with an extra variable you don't need.

Also, using Type Conversion is better in many cases. Example: variable 'X' is of data type Date, but you need to evaluate it in a string. Using CStr(X) allows you to do that, without changing the data type of the variable. This means you would have no problems evaluating 'X' as a date again later in your code.


Randy's implementation of CStr is the correct way to do the conversion, but his code only returns the left-most character.
Yeah... I wanted to give a concrete example of using Type Conversion. Should have explained a little better in my post- your example takes it a step further that I think makes it clearer:beerchug:

Dave
08-01-2008, 06:01 AM
Thank you's for the info. Have a nice day. Dave