VBA CSTR

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 codingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more. 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.

VBA CSTR

You are free to use this image on your website, templates etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA CSTR (wallstreetmojo.com)

What Does CSTR Function Do in VBA?

Have you ever thought of converting a different expression to Strings in VBAStrings In VBAString functions in VBA do not replace the string; instead, this function creates a new string. There are numerous string functions in VBA, all of which are classified as string or text functions.read more? If you have a doubt, is that possible? Then the answer is absolute YES!!!

“CSTR” is a function which coverts different format expression to String format in VBA. With the 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.

cstr formula

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 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.

You can download this VBA CStr Excel Template here – VBA CStr Excel Template

Example #1

For example, 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
cstr example 1.1

 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 like 855, it is no longer an Integer Date Type in VBAInteger Date Type In VBAIn VBA, an integer is a data type that may be assigned to any variable and used to hold integer values. In VBA, the bracket for the maximum number of integer variables that can be kept is similar to that in other languages. Using the DIM statement, any variable can be defined as an integer variable.read more. It is now in String Data Type.

cstr example 1.2

Example #2

For example, look at an example of VBA Boolean Data TypeVBA Boolean Data TypeBoolean is an inbuilt data type in VBA used for logical references or logical variables. The value this data type holds is either TRUE or FALSE and is used for logical comparison. The declaration of this data type is similar to all the other data types.read more 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
example 1.3

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.

example 2.2

Example #3

For example, 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
VBA cstr example 3.1

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 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.

example 3.2

Recommended Articles

This 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 examplesBelow are some useful excel articles related to VBA –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>