VBA String Data Type in Excel

String Data Type in Excel VBA

String Data Type in Excel VBA explained with syntax and examples.String Data Type in Excel VBA explained with syntax and examples. Where string is nothing but sequence of characters. We use VBA data type string to store text. Text includes not only characters but also numbers, spaces and special characters. The string contains double-quotation-mark literal. It is embedded within a string.

There are two types of strings.

1. Variable-length String and
2. Fixed-length String

Variable-length String:

The Variable-length string can contain up to approximately 2 billion characters.The variable-length string occupies 10 bytes (80 bits) and the memory that is required for the string itself.

Fixed-length String:

The Fixed-length string can contain 1 to approximately 64 K (64,000) characters. The fixed-length string occupies the amount of memory required by the string.

Note: It has type declaration character. It is nothing but character appended to a variable instead of data type. The type declaration character for single VBA data type is the explanation point($).

String Data Type Syntax in Excel VBA

Let us see Data Type String Syntax in Excel VBA.

Dim VariableName as String

Where VariableName represents the name of the variable.
and String represents the type of data.

Example on the String(variable length) in Excel VBA

Here is an example on the String (variable length) in Excel VBA.

Sub VBAF1_String_DataType()
    'Integer Variable Declaration
    Dim sValue As String
    sValue = "Sample Text"
End Sub

Convert an Expression to String Data Type in Excel VBA

You can convert an expression to String VBA data type using VBA CStr function. Click on the following link to learn about CStr function and complete tutorial and examples.

VBA CStr Function

Also read about all other data types.

VBA Data Types

Leave a Comment

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