Excel VBA Str Function

VBA Str Function: The Str function in VBA converts the given number into a string representation of that number.

Syntax

Str (Number)

Parameter

Number (required) – This parameter represents the numeric value that you want to convert to string.

Return

This function returns a string value after converting the specified number into String data type.

Example 1

Sub StrFunction_Example1()
 ' Converting the numeric values into strings.
 Dim str_val As String
 str_val = Str(500)
 ' The variable str_val will return the String " 500".
 Cells(1, 1).Value = str_val
 End Sub 

Output

500

VBA Str Function

Example 2

Sub StrFunction_Example2()
 ' Converting the numeric values into strings.
 Dim str_val As String
 'with negtive values
 str_val = Str(-500)
 ' The variable str_val will return the String " -500".
 Cells(1, 1).Value = str_val
 End Sub 

Output

-500

VBA Str Function

Example 3

Sub StrFunction_Example3()
 ' Converting the numeric values into strings.
 Dim str_val As String
 'with string values
 str_val = Str("Hello VBA")
 ' The variable str_val will return type mismatch run time error.
 Cells(1, 1).Value = str_val
 End Sub 

Output

VBA Str Function