Excel VBA CStr Function
VBA CStr Function: The CStr function in VBA converts an expression into a string data type.
Syntax
CStr (Expression)
Parameter
Expression (required) – This parameter represents the expression that you want to convert to a String.
Return
This function returns a String value after converting the expression into a string data type.
Example 1
Sub CStrFunction_Example1() ' Converting the Integer, Boolean and Date expressions into Strings Dim str As String str = CStr(20) ' The variable str will return the string "20" Cells(1, 1).Value = str End Sub
Output
20
Example 2
Sub CStrFunction_Example2() ' Converting the Integer, Boolean and Date expressions into Strings Dim str As String str = CStr(12 / 9 / 2020) ' The variable str will return the string "0.00066006600660066" Cells(1, 1).Value = str End Sub
Output
0.00066006600660066
Example 3
Sub CStrFunction_Example3() ' Converting the Integer, Boolean and Date expressions into Strings Dim str As String 'to represent date use # before and after the expression str = CStr(#12/9/2020#) ' The variable str will return the string "12/9/2020" Cells(1, 1).Value = str End Sub
Output
12/9/2020
Example 4
Sub CStrFunction_Example4() ' Converting the Integer, Boolean and Date expressions into Strings Dim str As String str = CStr(True) ' The variable str will return the string "TRUE" Cells(1, 1).Value = str End Sub
Output
TRUE