The VBA Str Function
The VBA Str function converts a supplied number into a string representation of that number.
The syntax of the function is:
Str( Number )
Where the Number argument is the number that you want to convert into a string.
Note: the Str function always reserves the first character of the returned string for the sign of the supplied Number. Therefore:
- Positive numbers have a leading space (the "+" sign is implied);
- Negative numbers start with the "-" sign.
VBA Str Function Examples
Dim str1 As String
Dim str2 As String
str1 = Str( 500 )str2 = Str( -10.5 )
Note that, in the above examples:
- When the positive value 500 is supplied to the Str function, the returned string, " 500" has a leading space;
- When a negative value is supplied to the Str function, the returned string starts with the "-" character.
VBA Str Function Error
If the VBA Str function is supplied with a value that cannot be interpreted as a number, it will return the error:
Run-time error '13': Type mismatch