Excel VBA String Add: Concatenate Text the Right Way
In this article
To add strings together in Excel VBA, use the ampersand operator (&) between the text values or variables you want to join. For example, fullName = firstName & " " & lastName combines a first name, a space, and a last name into one string. The plus sign (+) also works in many cases, but the ampersand is the safer and more predictable choice because it always treats both sides as text, while + can trigger numeric addition if the values look like numbers. This guide walks through the correct syntax, common pitfalls, and practical patterns for building strings in VBA.
What You'll Need
- Excel installed on Windows or Mac (VBA works on both, though the editor looks slightly different).
- A workbook saved as
.xlsm(macro-enabled) if you plan to keep the code. - Access to the VBA editor: press
Alt + F11on Windows, orFn + Option + F11on Mac. - Basic familiarity with inserting a module and running a macro.
Step-by-Step
- Open the VBA editor. In Excel, press
Alt + F11(Windows) orFn + Option + F11(Mac). The Visual Basic Editor opens in a separate window. Menu labels and ribbon positions can differ slightly by Excel version and region. - Insert a module. In the editor, go to
Insert > Module. A blank code window appears. All your string-building code will go here. - Write a simple concatenation. Type the following into the module:
The ampersand joins the two variables and the literal space between them. TheSub AddStrings() Dim firstName As String Dim lastName As String Dim fullName As String firstName = "Alex" lastName = "Morgan" fullName = firstName & " " & lastName MsgBox fullName End SubMsgBoxdisplays the result. - Run the macro. Press
F5while the cursor is inside the subroutine, or useRun > Run Sub/UserForm. A message box should showAlex Morgan. If you see an error, check that you used straight quotes, not curly quotes copied from a document. - Add a third piece of text. Extend the line to include a middle initial or a title:
EachfullName = "Dr. " & firstName & " " & lastName&chains another value. VBA evaluates left to right, so the order of the pieces determines the final output. - Join a cell value with text. To read from a worksheet and build a string:
This pulls whatever is in A1, wraps it with a greeting, and writes the result to B1. If A1 is empty, the result isDim greeting As String greeting = "Hello, " & Range("A1").Value & "! Welcome." Range("B1").Value = greetingHello, ! Welcome.— a common source of awkward output, so validate the cell first if needed. - Convert numbers to text before joining. If you concatenate a number directly, VBA converts it automatically, but the format may not match what you expect (for example,
0.1 + 0.2style rounding issues). UseCStr()orFormat()to control the output:
This producesDim total As Double total = 1234.5 MsgBox "Total: " & Format(total, "$#,##0.00")Total: $1,234.50instead of a raw number. - Build strings in a loop. To accumulate text across rows:
Dim i As Long Dim result As String For i = 1 To 5 result = result & Cells(i, 1).Value & vbCrLf Next i MsgBox resultvbCrLfinserts a line break. Note thatresultstarts as an empty string, so the first iteration simply adds the first value. - Use the
&=shorthand. VBA supportsresult &= newTextas a shorter form ofresult = result & newText. It behaves identically and keeps long loops readable. - Test with the Immediate Window. Press
Ctrl + Gto open the Immediate Window, type? "A" & "B", and press Enter. It printsAB. This is the fastest way to check a concatenation expression without running a full macro.
Common Mistakes
- Using
+instead of&. If both operands look numeric,+adds them (e.g.,"1" + "2"gives3), not"12". Always use&for text joining. - Forgetting spaces.
firstName & lastNameproducesAlexMorgan. Add" "between values where a space belongs. - Missing separators in loops. Building a comma-separated list without adding
", "yields one long run-on string. Append the separator inside the loop. - Curly quotes. Copying code from Word or a website can insert smart quotes (
“ ”) that VBA rejects with a compile error. Retype quotes directly in the editor. - Assuming
Nullbehaves like an empty string. Concatenating aNullvalue (common from database queries) makes the entire resultNull. UseNz()or anIfcheck to substitute an empty string. - Ignoring leading/trailing spaces in cell values. A cell containing
"Alex "with a trailing space produces double spaces when joined. Wrap values inTrim().
Tips
- Use
vbCrLffor Windows line breaks andvbLffor a plain line feed;vbCrLfis the safe default for message boxes and cell output. - For readability with many pieces, break the expression across lines using the line-continuation character
_at the end of each line. Join()is handy when you already have an array of strings:Join(myArray, ", ")produces a delimited string in one step.String(number, character)repeats a character, e.g.,String(10, "-")gives a ten-dash divider.- Use
Len()to verify the final length when building fixed-width output. - Keep concatenations inside variables rather than writing to cells repeatedly; write once at the end for speed.
Troubleshooting
- Compile error: Expected: end of statement. Usually a curly quote or a missing ampersand. Retype the line in the VBA editor.
- Result shows
Null. One of the joined values isNull. Wrap each value withNz(value, "")or test withIsNull(). - Numbers appear in scientific notation. Use
Format()orCStr()to control how numbers convert to text. - Macro runs but nothing happens. Confirm the cursor is inside the correct subroutine, and check that the module is in the right workbook. Macros in
PERSONAL.XLSBmay not be visible where you expect. - Security prompt blocks the macro. Enable macros for the file through Excel's Trust Center, or save the workbook in a trusted location. Do not disable security settings globally.
- Output has extra spaces. Apply
Trim()to each source value before concatenating.
Frequently Asked Questions
What is the difference between & and + in VBA?
& always concatenates text. + concatenates only when at least one operand is text; if both are numeric strings, it adds them. Use & for all string building.
How do I add a new line inside a VBA string?
Use vbCrLf between the pieces: "Line 1" & vbCrLf & "Line 2". This works in message boxes and in cell values with wrap text enabled.
Can I concatenate a range of cells in one line?
Not directly with &. Loop through the range and accumulate with &=, or use Join() after loading values into an array.
Why does my concatenated number lose its leading zeros?
VBA converts numeric-looking text to a number during concatenation. Format the source with Format(value, "00000") or store it as text before joining.
Does string concatenation work the same on Mac?
Yes. The operators and functions are identical; only the editor shortcuts differ. Save the workbook as .xlsm so the macro persists.
How do I join an array of strings with a separator?
Use Join(arrayName, ", "). It returns a single string with the separator between elements and no trailing separator.