String Data Type

There are two types of string variables in VBA fixed length and variable length


Fixed Length

The fixed length string data type can contain between 1 and 65,400 characters.
The maximum length is 65,535 characters
This data type uses ? (length of the string)
The default value is the number of spaces equal to the length of the string.
A fixed length string is defined as follow:

Dim sFirstName As String * 20 

   sFirstName = "Monday"
   Call Msgbox("""" & sFirstName & """")

The above code displays the following message "Russell ".
This shows that the content is padded with spaces in order to reach the fixed length.


These are declared with a specified number of characters.
When you declare a variable to be of data type string you can specify the maximum length of that string
Working with fixed length strings is slightly more efficient than working with variable length strings.
Fixed length strings are often passed into Windows APIs.


Variable Length

The variable length string data type can contain between 0 and 2 billion characters.
This data type uses ? (10 + length of the string)
The default value is the empty string "".
This data type automatically increases and decreases in size depending on the number of characters.


A variable length string is defined as follows:

Dim sFirstName As String 

This type of string variable can hold string of any length.
This type is used most of the time.


A variable length string is assigned a zero-length string (""), empty string or nullstring by default.
This is not to be confused with the empty value of an unassigned Variant.
A zero-length string is neither empty (as it has an assigned value) not NULL.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext