Excel VBA CSTR
We work with lots of conversion function in our work area in excel. Such a function is CSTR function in VBA. It is basically a data type conversion function. So if I said that we want to convert a number to a string how will we do that in VBA? We do this with the help of this VBA CSTR function. VBA CSTR function converts any data type variable to string data type.
Let us learn about VBA CSTR function in detail. VBA CSTR function converts any data type to string type. For example, normally 1234 is a numeric value and ABC is a string value which is pretty simple. But if we want to change the data type for number 1234 from integer to string we use CSTR function. And as said above it is a string conversion function. It takes a single argument in it.
Formula For CSTR function in Excel VBA
Let us look below the formula for CSTR function in VBA.
Now expression can be any data type it could be integer long or Boolean etc. CSTR function will change the data type to String for the given expression regardless of what was the previous data type for the expression of itself. Now let us use this function in a few examples and see how this function actually works.
How to Use Excel VBA CSTR Function?
We will learn how to use a VBA CSTR function with few examples in Excel.
VBA CSTR – Example #1
For example in true or false results the data type for them is Boolean. But what if we want them to be string data type.
Follow the below steps to use CSTR function in Excel VBA.
Step 1: Open VB Editor from the developer’s tab as shown in the screenshot below.
Step 2: In the VBA editor insert a module from the reference to the screenshot below.
Step 3: Double click on the module and let’s begin our macro with a name as shown below.
Code:
Sub sample() End Sub
Step 4: Declare a variable as data type as Boolean and store value true in it.
Code:
Sub sample() Dim A As Boolean A = True End Sub
Step 5: Let us first display the data type of the variable A using the TypeName Function as follows.
Code:
Sub sample() Dim A As Boolean A = True MsgBox (TypeName(A)) End Sub
Step 6: Run the code by hitting F5 or Run button and see the output.
Step 7: Right now the data type for the variable A is Boolean now let us change it to a string using VBA CSTR function as follows.
Code:
Sub sample() Dim A As Boolean A = True MsgBox (TypeName(A)) Dim B As String B = CStr(A) End Sub
We defined another variable and store the value of A converting it to a string using CSTR function.
Step 8: Now display both the data type of variable B and the value stored in B using the message box function as follows.
Code:
Sub sample() Dim A As Boolean A = True MsgBox (TypeName(A)) Dim B As String B = CStr(A) MsgBox B MsgBox (TypeName(B)) End Sub
Step 9: Re-run the above code and see the last two result as follows.
The value stored in B is the value of A which is “TRUE”. Now let us see the data type for variable B.
Though A had a Boolean value in it we converted it to String using the CSTR function.
VBA CSTR – Example #2
Now let us convert the very basic data type which is an integer in the same way above. Follow the below steps to use CSTR function in Excel VBA.
Step 1: Write another macro name as follows in the same module.
Code:
Sub Sample1() End Sub
Step 2: Declare two variables one as integer and another as a string.
Code:
Sub Sample1() Dim A As Integer Dim B As String End Sub
Step 3: Store any random value in Variable A, keep in mind it is integer data type, so provide an integer value as follows.
Code:
Sub Sample1() Dim A As Integer Dim B As String A = 255 End Sub
Step 4: Now in Variable B convert A to a string using the CSTR function as shown below.
Code:
Sub Sample1() Dim A As Integer Dim B As String A = 255 B = CStr(A) End Sub
Step 5: Let us display the value of variable B as well as the data type of variable B as follows.
Code:
Sub Sample1() Dim A As Integer Dim B As String A = 255 B = CStr(A) MsgBox B MsgBox (TypeName(B)) End Sub
Step 6: Run the code by hitting F5 or Run button and see the output.
The value stored in variable B is 255 which should be an integer but let us see what we get as the data type for variable B, We converted value 255 which was an integer as a string using CSTR function.
VBA CSTR – Example #3
Now let us try a complex data type which is Date data type. We will convert a date data type value to string. Follow the below steps to use CSTR function in Excel VBA.
Step 1: The third macro name can be as follows.
Code:
Sub Sample2() End Sub
Step 2: Two variables need to declare one as Date data type and another as a string as shown in the image below.
Code:
Sub Sample2() Dim A As Date Dim B As String End Sub
Step 3: Store a date in Variable A as follows.
Code:
Sub Sample2() Dim A As Date Dim B As String A = #1/1/2019# End Sub
Step 4: Now convert the date value in A as a string using CSTR function and store the value in variable B as shown below.
Code:
Sub Sample2() Dim A As Date Dim B As String A = #1/1/2019# B = CStr(A) End Sub
Step 5: Now let us display both the value and data type of variable B as follows.
Code:
Sub Sample2() Dim A As Date Dim B As String A = #1/1/2019# B = CStr(A) MsgBox B MsgBox (TypeName(B)) End Sub
Step 6: Run the code by hitting F5 or Run button and see the output.
The value we see above is Date which is stored in variable B but when we press ok we see the data type of the above value. Though the value was the date the data type is a string.
Things to Remember
- CSTR is a conversion function.
- CSTR uses only a single argument.
- VBA CSTR function converts any value of data type to string data type.
Recommended Articles
This is a guide to VBA CSTR Function. Here we discuss how to use Excel VBA CStr function to convert the value to String Data Type along with practical examples and downloadable excel template. You can also go through our other suggested articles –