How to use the VBA STR Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA STR Function (Syntax + Example)

The VBA STR function is listed under the data type conversion category of VBA functions. When you use it in VBA code, it converts a number into a string. In simple words, it can convert a number into a text which represents the number.

Syntax

Str(Number)

Arguments

  • Number: A number for which you want to convert into a string.

Example

To practically understand how to use VBA STR function, you need to go through the below example where we have written a vba code by using it:

Sub example_STR()
Range("B1").Value = Str(Range("A1"))    
End Sub

In the above code, we have used the STR to convert the numeric value in the cell A1 into a string and return the result in the cell B1.

Notes

  • It adds a leading space every time it converts a number to a string for the sign of the number.
  • If the value specified is a value other than a number or a number that can’t be recognized as a number, VBA will return the run-time 13 error.

Related Functions

CBOOL | CBYTE | CCUR | CDATE | CDBL | CDEC | CHR | CINT | CLNG | CSNG | CSTR | CVAR | FORMATCURRENCY | FORMATDATETIME | FORMATNUMBER | FORMATPERCENT | HEX | OCT | VAL