VBA STR Function

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

What is VBA STR Function

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.

how to use it

To use VBA's STR function you need to understand its syntax and arguments:

Syntax

Str(Number)

Arguments

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

Example to use STR Function in VBA

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

Below are some important points which you need to take care while using STR function in VBA.

  • 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

About the Author

puneet one point one

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.