Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA String Add: Concatenate Text the Right Way

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 + F11 on Windows, or Fn + Option + F11 on Mac.
  • Basic familiarity with inserting a module and running a macro.

Step-by-Step

  1. Open the VBA editor. In Excel, press Alt + F11 (Windows) or Fn + 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.
  2. Insert a module. In the editor, go to Insert > Module. A blank code window appears. All your string-building code will go here.
  3. Write a simple concatenation. Type the following into the module:
    Sub AddStrings()
        Dim firstName As String
        Dim lastName As String
        Dim fullName As String
        firstName = "Alex"
        lastName = "Morgan"
        fullName = firstName & " " & lastName
        MsgBox fullName
    End Sub
    The ampersand joins the two variables and the literal space between them. The MsgBox displays the result.
  4. Run the macro. Press F5 while the cursor is inside the subroutine, or use Run > Run Sub/UserForm. A message box should show Alex Morgan. If you see an error, check that you used straight quotes, not curly quotes copied from a document.
  5. Add a third piece of text. Extend the line to include a middle initial or a title:
    fullName = "Dr. " & firstName & " " & lastName
    Each & chains another value. VBA evaluates left to right, so the order of the pieces determines the final output.
  6. Join a cell value with text. To read from a worksheet and build a string:
    Dim greeting As String
    greeting = "Hello, " & Range("A1").Value & "! Welcome."
    Range("B1").Value = greeting
    This pulls whatever is in A1, wraps it with a greeting, and writes the result to B1. If A1 is empty, the result is Hello, ! Welcome. — a common source of awkward output, so validate the cell first if needed.
  7. 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.2 style rounding issues). Use CStr() or Format() to control the output:
    Dim total As Double
    total = 1234.5
    MsgBox "Total: " & Format(total, "$#,##0.00")
    This produces Total: $1,234.50 instead of a raw number.
  8. 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 result
    vbCrLf inserts a line break. Note that result starts as an empty string, so the first iteration simply adds the first value.
  9. Use the &= shorthand. VBA supports result &= newText as a shorter form of result = result & newText. It behaves identically and keeps long loops readable.
  10. Test with the Immediate Window. Press Ctrl + G to open the Immediate Window, type ? "A" & "B", and press Enter. It prints AB. 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" gives 3), not "12". Always use & for text joining.
  • Forgetting spaces. firstName & lastName produces AlexMorgan. 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 Null behaves like an empty string. Concatenating a Null value (common from database queries) makes the entire result Null. Use Nz() or an If check 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 in Trim().

Tips

  • Use vbCrLf for Windows line breaks and vbLf for a plain line feed; vbCrLf is 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 is Null. Wrap each value with Nz(value, "") or test with IsNull().
  • Numbers appear in scientific notation. Use Format() or CStr() 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.XLSB may 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.