WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA CSTR

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

vba-cstr

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

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

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 VBA. It is now in String Data Type.

cstr example 1.2

Example #2

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

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 examples. Below are some useful excel articles related to VBA –

  • Convert String in VBA
  • Excel VBA StrComp
  • VBA Like Operator
  • Count Function in VBA
0 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download VBA CStr Excel Template

New Year Offer - VBA Training Course (6 courses, 35+ hours video) View More