VBA CStr Function

VBA CStr Function

Objective of VBA CStr Function:

VBA CStr Function is categorized as a Data Type Conversion function. It is a built-in function in Excel VBA. This VBA CStr function converts an expression to a String data type.
This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA CStr Function in any number of times in any number of procedures or functions. In the following section you learn many topics. Like what is the syntax and parameters of the CStr function, where we can use this VBA CStr Function and it’s real-time examples.

Syntax of VBA CStr Function:

The syntax of the CStr Function in VBA is

CStr(Expression)

The CStr function returns a string data type value.

Parameters or Arguments:

The CStr function has one argument in Excel VBA.
where
Expression:It is a mandatory argument. An expression argument represents a value. It is used to convert the value to a string value.

Where we can apply or use VBA CStr Function?

We can use this CStr Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Convert a String(100) to String Data Type

Here is a simple example of the VBA CStr function. This below example specified an expression(100) converts to a string type value.

'Convert a String(100) to String Data Type
Sub VBA_CStr_Function_Ex1()

    'Variable declaration
    Dim sValue As String
    Dim dResult As String
    
    sValue = 100
        
    dResult = CStr(sValue)
        
    MsgBox "String(100) to String Data Type : " & dResult, vbInformation, "VBA CStr Function"
    
End Sub

Output: Here is the screen shot of the first example output.
Example of VBA CStr Function

Example 2: Convert a Value(12345.678) to String Data Type

Here is a simple example of the VBA CStr function. This below example specified an expression(12345.678) converts to a string type value.

'Convert a Value(12345.678) to String Data Type
Sub VBA_CStr_Function_Ex2()

    'Variable declaration
    Dim sValue As Double
    Dim dResult As String
    
    sValue = 12345.678
        
    dResult = CStr(sValue)
        
    MsgBox "Value(12345.678) to String Data Type : " & dResult, vbInformation, "VBA CStr Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA CStr Function

Example 3: Convert a Value(True) to String Data Type

Here is a simple example of the VBA CStr function. This below example specified an expression(True) converts to a string type value. It returns true as output.

'Convert a Value(True) to String Data Type
Sub VBA_CStr_Function_Ex3()

    'Variable declaration
    Dim sValue
    Dim dResult As String
    
    sValue = True
        
    dResult = CStr(sValue)
        
    MsgBox "Value(True) to String Data Type : " & dResult, vbInformation, "VBA CStr Function"
End Sub

Output: Here is the screen shot of the third example output.
VBA Convert Value to String using cStr Function

Example 4: Convert a Value(False) to String Data Type

Here is a simple example of the VBA CStr function. This below example specified an expression(False) converts to a string type value. It returns false as output.

'Convert a Value(False) to String Data Type
Sub VBA_CStr_Function_Ex4()

    'Variable declaration
    Dim sValue
    Dim dResult As String
    
    sValue = False
        
    dResult = CStr(sValue)
        
    MsgBox "Value(False) to String Data Type : " & dResult, vbInformation, "VBA CStr Function"
End Sub

Output: Here is the screen shot of the fourth example output.
VBA CStr Function

Instructions to use Macro Codes

Here are the step by step instructions to use the CStr Function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of All Type Conversion Functions in Excel VBA:

Here are the list of Data Type Conversion functions. And also find its syntax and return type. Please click on below link to go and see the related VBA Functions.

Function Description Syntax Return Type
VBA CBool VBA CBool function converts an expression to a Boolean data type. CBool(Expression) Boolean
VBA CByte VBA CByte function converts an expression to a Byte data type. CByte(Expression) Byte
VBA CCur VBA CCur function converts an expression to a Currency data type. CCur(Expression) Currency
VBA CDate VBA CDate function converts an expression to a Date data type. CDate(Expression) Date
VBA CDbl VBA CDbl function converts an expression to a Double data type. CDbl(Expression) Double
VBA CDec VBA CDec function converts an expression to a Decimal data subtype. CDec(Expression) Decimal
VBA CInt VBA CInt function converts an expression to an Integer data type. CInt(Expression) Integer
VBA CLng VBA CLng function converts an expression to a Long data type. CLng(Expression) Long
VBA CSng VBA CSng function converts an expression to a Single data type. CSng(Expression) Single
VBA CStr VBA CStr function converts an expression to a String data type. CStr(Expression) String
VBA CVar VBA CVar function converts an expression to a Variant data type. CVar(Expression) Variant

You can use FORMAT function to format the string.

VBA Data Type Conversion Functions

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

List of VBA Functions

Here are few more examples on using cStr function in VBA:

1 thought on “VBA CStr Function”

  1. Pretty nice post. I just stumbled upon your blog and wished to mention that I’ve
    really loved surfing around your blog posts. In any case I’ll be subscribing for your rss
    feed and I’m hoping you write again very soon!

Leave a Comment

Your email address will not be published. Required fields are marked *