Declaring and assigning strings
Declare a string constant
Section titled “Declare a string constant”Const appName As String = "The App For That"Declare a variable-width string variable
Section titled “Declare a variable-width string variable”Dim surname As String 'surname can accept strings of variable lengthsurname = "Smith"surname = "Johnson"Declare and assign a fixed-width string
Section titled “Declare and assign a fixed-width string”'Declare and assign a 1-character fixed-width stringDim middleInitial As String * 1 'middleInitial must be 1 character in lengthmiddleInitial = "M"
'Declare and assign a 2-character fixed-width string `stateCode`,'must be 2 characters in lengthDim stateCode As String * 2stateCode = "TX"Declare and assign a string array
Section titled “Declare and assign a string array”'Declare, dimension and assign a string array with 3 elementsDim departments(2) As Stringdepartments(0) = "Engineering"departments(1) = "Finance"departments(2) = "Marketing"
'Declare an undimensioned string array and then dynamically assign with'the results of a function that returns a string arrayDim stateNames() As StringstateNames = VBA.Strings.Split("Texas;California;New York", ";")
'Declare, dimension and assign a fixed-width string arrayDim stateCodes(2) As String * 2stateCodes(0) = "TX"stateCodes(1) = "CA"stateCodes(2) = "NY"Assign specific characters within a string using Mid statement
Section titled “Assign specific characters within a string using Mid statement”VBA offers a Mid function for returning substrings within a string, but it also offers the Mid Statement which can be used to assign substrings or individual characters withing a string.
The Mid function will typically appear on the right-hand-side of an assignment statement or in a condition, but the Mid Statement typically appears on the left hand side of an assignment statement.
Dim surname As Stringsurname = "Smith"
'Use the Mid statement to change the 3rd character in a stringMid(surname, 3, 1) = "y"Debug.Print surname
'Output:'SmythNote: If you need to assign to individual bytes in a string instead of individual characters within a string (see the Remarks below regarding the Multi-Byte Character Set), the MidB statement can be used. In this instance, the second argument for the MidB statement is the 1-based position of the byte where the replacement will start so the equivalent line to the example above would be MidB(surname, 5, 2) = "y".
Assignment to and from a byte array
Section titled “Assignment to and from a byte array”Strings can be assigned directly to byte arrays and visa-versa. Remember that Strings are stored in a Multi-Byte Character Set (see Remarks below) so only every other index of the resulting array will be the portion of the character that falls within the ASCII range.
Dim bytes() As ByteDim example As String
example = "Testing."bytes = example 'Direct assignment.
'Loop through the characters. Step 2 is used due to wide encoding.Dim i As LongFor i = LBound(bytes) To UBound(bytes) Step 2 Debug.Print Chr$(bytes(i)) 'Prints T, e, s, t, i, n, g, .Next
Dim reverted As Stringreverted = bytes 'Direct assignment.Debug.Print reverted 'Prints "Testing."Remarks
Section titled “Remarks”Strings are a Reference type and are central to most programming tasks. Strings are assigned text, even if the text happens to be numeric. Strings can be zero-length, or any length up to 2GB. Modern versions of VBA store Strings internally using a Byte array of Multi-Byte Character Set bytes (an alternative to Unicode).