Excel VBA CSTR Function
CSTR in VBA is a data type conversion function which is used to convert any value provided to this function to string, even if the given input is in integer or float value this function will convert the data type of the value to a string data type, so the return type of this function is a string.
If we need to convert any value to string data type in VBA how do we go about this? For this, in VBA we have a function called “CSTR”. In this article, we will guide you through the methodology of the “CSTR” function in VBA.
The string is the data type that holds any kind of String values. When we say string it generally refers to text values but that is not true with VBA coding. A string can hold any order of characters as data. For example “Hello” is treated as String, “123456” is treated as a string, “12-04-2019” is treated as a string. Like this String data type can hold of any order of characters.
What Does CSTR Function Do in VBA?
Have you ever thought of converting a different expression to Strings in VBA? If you have a doubt that is it possible? Then the answer is absolute YES!!!
“CSTR” is a function which coverts different format expression to String format in VBA. With CSTR function we can convert the provided expression value to String data type.
VBA CSTR Syntax
Below is the syntax of the Excel VBA CSTR function.
The syntax of the CSTR function includes only one argument.
4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
Expression: It is the targeted value or cell value we are trying to change to String data type.
The value could be any data type, CSTR goes ahead and converts to String data type. The common data types we usually convert are Integer, Boolean, and Date to String data types.
How to Use VBA CSTR Function in Excel?
Now we will see some of the examples of Excel VBA CSTR function.
For an example look at the below code.
Sub CSTR_Example1() Dim NumericValue As Integer Dim StringResult As String NumericValue = 855 StringResult = CStr(NumericValue) MsgBox StringResult End Sub
Firstly I have assigned the Integer data type to the variable “NumericValue” as 855. Now the variable “NumericValue” holds Integer data type. With another variable “StringResult” assigned the formula CSTR to convert Integer Data Type to String Data Type.
CSTR converted the integer number to String Data Type. Even though we can still see the number as 855 it is no longer an Integer Date Type in VBA, it is now in String Data Type.
For example, look at an example of VBA Boolean Data Type Conversion.
Sub CSTR_Example2() Dim Val1 As Boolean Dim Val2 As Boolean Val1 = True Val2 = False MsgBox CStr(Val1) & vbNewLine & CStr(Val2) End Sub
In the above code, I have declared two variables as Boolean.
Dim Val1 As Boolean Dim Val2 As Boolean
In the next line, I have assigned Boolean values as TRUE & FALSE.
Val1 = True Val2 = False
At this point in time, both the variables are Boolean data type. Now in this example, I have applied the VBA CSTR function to convert this Boolean data type to a String Data Type.
For example, look at the example of Date Data Type Conversion to String Data Type.
Sub CSTR_Example3() Dim Date1 As Date Dim Date2 As Date Date1 = #10/12/2019# Date2 = #5/14/2019# MsgBox CStr(Date1) & vbNewLine & CStr(Date2) End Sub
I have declared two variables as Date.
Dim Date1 As Date Dim Date2 As Date
Next line I have assigned the Date values as 10-12-2019 & 05-14-2019 respectively.
Date1 = #10/12/2019# Date2 = #5/14/2019#
At this point in time, both the variables are Date data type. Now in the next line, I have applied CSTR function to convert the Date data type to the String Data Type. Like CSTR function used to convert any other data type to String Data Type.
This has been a guide to VBA CStr. Here we learn how to use VBA CStr function to convert the value to String Data Type along with some simple to advanced examples. Below are some useful excel articles related to VBA –