VBA CSTR
Last Updated :
-
Blog Author :
Edited by :
Reviewed by :
Table Of Contents
Excel VBA CSTR Function
CSTR in VBA is a data type conversion function that one may use to convert any value provided to this function to a string. For example, even if the given input is an integer or float value, this function will convert the value's data type to a string data type, so the return type of this function is a string.
How do we go about this if we need to convert any value to string data type in VBA? 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 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 and "12-04-2019" as a string. This String data type can hold 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, is that possible? Then the answer is an absolute YES!
CSTR is a function covering different format expressions to String format in VBA. With the CSTR function, we can convert the provided expression value to the 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.
Expression: It is the targeted value or cell value we are trying to change to the String data type.
The value could be any data type. So, 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 examples of the Excel VBA CSTR function.
Example #1
Look at the below code.
Code:
Sub CSTR_Example1() Dim NumericValue As Integer Dim StringResult As String NumericValue = 855 StringResult = CStr(NumericValue) MsgBox StringResult End Sub
Firstly, we assigned the Integer data type to the variable "NumericValue" as 855. Now, the variable "NumericValue" holds the Integer data type. 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. So, even though we can see the number 855, it is no longer an Integer Date Type in VBA. Instead, it is now in the String data type.
Example #2
Look at an example of VBA Boolean Data Type Conversion.
Code:
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, we have assigned Boolean values as TRUE and FALSE.
Val1 = True Val2 = False
At this point, both variables are Boolean data types. Therefore, we have applied the VBA CSTR function in this example to convert this Boolean data type to a String data type.
Example #3
Look at the example of Date data type conversion to String data type.
Code:
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
We have declared two variables as Date.
Dim Date1 As Date Dim Date2 As Date
Next line, we have assigned the Date values as 10-12-2019 and 05-14-2019, respectively.
Date1 = #10/12/2019# Date2 = #5/14/2019#
At this point, both the variables are Date data types. In the next line, we have applied the 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.
Recommended Articles
This article has been a guide to VBA CStr. Here, we learn how to use the 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: -