VBA CStr function

CStr Function Description

The CStr function converts any variable (number, boolean etc.) to a string variable. This functions achieves a similar result as adding the variable to an empty String (vbNullString).

Syntax

The syntax for the CStr function in VBA is:

1
CStr( expression )

Parameters

value
An expression / variable that is to be converted to String data type.

Other Notes

The CStr function can be superseded by a simple string concatenation e.g.:

1
2
3
4
5
6
7
8
9
10
11
Debug.Print CStr(21123)
'Result: "21123" (String)
 
'Is similar to:
 
Debug.Print ("" & 21123)
'Result: "21123" (String)
 
'Or even better:
Debug.Print (vbNullString & 21123)
'Result: "21123" (String)

As you can read here it is recommended to use the vbNullString variable instead of an empty string ” “ for heavy VBA calculations.

Example usage

The CStr function can only be used in VBA code. Let’s look at some CStr function examples:

1
2
3
4
5
6
7
Debug.Print CStr(12345)
'Result: "12345" (String)
 
Dim tmp as Long
tmp = 10
Debug.Print CStr( tmp )
'Result: "10" (String)