Questions And Answers

More Tutorials

VBA Declaring and assigning strings

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 Byte
Dim example As String
example = "Testing."
bytes = example 'Direct assignment.
'Loop through the characters. Step 2 is used due to wide encoding.
Dim i As Long
For i = LBound(bytes) To UBound(bytes) Step 2
 Debug.Print Chr$(bytes(i)) 'Prints T, e, s, t, i, n, g, .
Dim reverted As String
reverted = bytes 'Direct assignment.
Debug.Print reverted 'Prints "Testing."

Declare a string constant

Const appName As String = "The App For That"

Declare a variable-width string variable

Dim surname As String 'surname can accept strings of variable length
surname = "Smith"
surname = "Johnson"


In this page (written and validated by ) you learned about VBA Declaring and assigning strings . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Declare and assign a fixed width string.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.