How to Build Complex Strings with Special Characters in VBA and VB.NET
Question details
The user needs to construct text strings containing both literal text and special character commands (such as line breaks) without the commands being treated as literal text.

- Product
- Microsoft Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Writing a macro or script that outputs multi-line messages, logs, or formatted text strings where dynamic variables and specific formatting are required.
- Observed behavior
- Special functions like Chr(10) are incorrectly treated as literal text and displayed as "Chr(10)" when placed inside quotation marks.
Ensure you have the Developer tab enabled in your spreadsheet software and have access to the Visual Basic for Applications (VBA) editor to test your string concatenation code.
Concatenate the Chr Function Outside Quotation Marks
Use the ampersand (&) operator to join literal strings with VBA functions. This ensures special characters execute correctly instead of displaying as plain text.
In VBA and VB.NET, any text enclosed in double quotation marks is interpreted literally. To insert dynamic functions like Chr(10) for a line-feed, you must end the quoted string, use the concatenation operator, insert the function, and then concatenate the rest of your string.
Press ALT + F11 in your spreadsheet application to launch the Visual Basic for Applications editor.
Create a variable to hold your formatted text, for example: Dim headString As String.
Type your literal text inside quotes, add the & symbol, insert your function, add another &, and continue with the next quoted string. Example: headString = "a" & Chr(10) & "b" & "b,c,d,e,f".

Utilize Built-in VBA Constants for Line Breaks
Instead of using the Chr function, you can use native VBA constants for line breaks which are easier to read and maintain.
Write and Execute VBA Macros Effortlessly in WPS Office
WPS Office provides a highly compatible VBA environment for its premium users, allowing you to run complex macros, manipulate strings, and automate tasks exactly as you would in Microsoft Excel.
- 1. Launch WPS Spreadsheet: Open WPS Office and create a new document or open an existing spreadsheet that requires automation.
- 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon menu and click on the 'Visual Basic' icon.
- 3. Write Your Macro: Insert a new Module, define your string variables, and use standard VBA concatenation to automate your workflow securely.

Frequently Asked Questions
Why is my Chr(10) command showing up as plain text?
If you enclose Chr(10) within double quotation marks (e.g., "Text Chr(10) Text"), the VBA compiler reads it as literal string characters rather than executing the function. It must be placed completely outside the quotes using the & operator.
Should I use & or + to concatenate strings in VB.NET?
You should always use the ampersand (&) operator to concatenate strings in VBA and VB.NET. While the plus sign (+) sometimes works, it can cause unintended mathematical addition or type mismatch errors if the strings contain numbers.
How do I include double quotation marks inside a VBA string?
To include a literal double quote within a string, you must type two double quotes together inside your quoted text, or concatenate the Chr(34) function. For example: myString = "She said, ""Hello!""".




