Skip to main content

Excel String data type

There are two kinds of strings: variable-length and fixed-length strings.

  • A variable-length string can contain up to approximately 2 billion (2^31) characters.
  • A fixed-length string can contain 1 to approximately 64 K (2^16) characters.

The codes for String characters range from 0–255. The first 128 characters (0–127) of the character set correspond to the letters and symbols on a standard U.S. keyboard. These first 128 characters are the same as those defined by the ASCII character set. The second 128 characters (128–255) represent special characters, such as letters in international alphabets, accents, currency symbols, and fractions.

The type-declaration character for String is the dollar ($) sign.

A double-quotation-mark can be embedded within a string literal in one of two ways:

  • Use two double-quotation-marks:
    Dim s As String
    s = "This string literal has an embedded "" in it."
  • Use the Chr function; character code 34 is a double-quotation-mark:
    Dim s As String
    s = "This string literal has an embedded " & Chr(34) & " in it."

A fixed-length string includes appended spaces or truncates as necessary:

Dim s As String * 3
Debug.Print Len(s) & " characters [" & s & "]" 'Prints 3 characters [   ]
s = "a"
Debug.Print Len(s) & " characters [" & s & "]" 'Prints 3 characters [a  ]
s = "abcdefghijklmnopqrstuvwxyz"
Debug.Print Len(s) & " characters [" & s & "]" 'Prints 3 characters [abc]

Leave a comment

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

Format your code: <pre><code class="language-vba">place your code here</code></pre>