EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Conversion Functions VBA CSTR
 

VBA CSTR

Madhuri Thakur
Article byMadhuri Thakur

VBA CSTR

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Formula For CSTR function in Excel VBA

Let us look below the formula for CSTR function in VBA.

Syntax of CSTR

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.

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

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.

VBA CSTR Example 1-1

Step 2: In the VBA editor insert a module from the reference to the screenshot below.

VBA CSTR Example 1-2

Step 3: Double click on the module and let’s begin our macro with a name as shown below.

Code:

Sub sample()

End Sub

VBA CSTR Example 1-3

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

VBA CSTR Example 1-4

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

VBA CSTR Example 1-5

Step 6: Run the code by hitting F5 or Run button and see the output.

Example 1-6

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

VBA CSTR Example 1-7

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

VBA CSTR Example 1-8

Step 9: Re-run the above code and see the last two result as follows.

True Value

The value stored in B is the value of A which is “TRUE”. Now let us see the data type for variable B.

String datatype

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

VBA CSTR Example 2-1

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

Declare two variables Example 2-2

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

VBA CSTR Example 2-3

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

VBA CSTR Example 2-4

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

VBA CSTR Example 2-5

Step 6: Run the code by hitting F5 or Run button and see the output.

Result of Example 2-6

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

VBA CSTR Example 3-1

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

VBA CSTR Example 3-2

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

Store a date in Variable A Example 3-3

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

VBA CSTR Example 3-4

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

VBA CSTR Example 3-5

Step 6: Run the code by hitting F5 or Run button and see the output.

Result of Example 3-6

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 –

  1. VBA Last Row
  2. VBA VLOOKUP
  3. VBA Web Scraping
  4. VBA XML

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA

Download VBA CSTR Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW