Excel VBA String Concatenation: Step-by-Step Guide
In this article
Excel VBA string concatenation means joining two or more text values into one string. The quickest way is the ampersand operator: fullName = firstName & " " & lastName. You can also use the + operator, but it can behave unpredictably with numbers, so stick with &. This guide walks you through the safe, repeatable way to build strings in VBA, with examples you can paste into the Visual Basic Editor.
What You'll Need
- Excel installed on Windows or Mac (the VBA editor is included in both).
- Basic familiarity with opening the Visual Basic Editor (Alt+F11 on Windows, or Developer tab > Visual Basic).
- A workbook where macros are enabled (save as .xlsm if you want to keep your code).
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 (Windows) or go to Developer > Visual Basic (Mac). If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to enable it. Menu labels can differ slightly by Excel version and region.
- Insert a new module. In the editor, choose Insert > Module. This is where you will write your code.
- Declare your variables. Use
Dimto declare string variables. For example:Dim firstName As String,Dim lastName As String,Dim fullName As String. Declaring types helps catch errors early. - Assign values to the variables. For example:
firstName = "Ada"andlastName = "Lovelace". You can also pull values from cells:firstName = Range("A1").Value. - Concatenate with the ampersand (&). Write
fullName = firstName & " " & lastName. The space in quotes is a literal space; without it you would get "AdaLovelace". - Add more pieces as needed. You can chain as many
&operations as you like:fullName = firstName & " " & lastName & " (" & department & ")". Each literal string goes in double quotes. - Output the result. Send it to a cell with
Range("B1").Value = fullName, or show it in a message box withMsgBox fullName. Run the macro with F5. - Handle numbers and dates explicitly. If you concatenate a number, VBA converts it using your system's regional settings. To control formatting, use
Format():result = "Total: " & Format(totalAmount, "#,##0.00"). For dates, useFormat(myDate, "yyyy-mm-dd")to avoid locale surprises. - Use the &= shortcut for building strings in loops. Instead of
myString = myString & newPart, you can writemyString &= newPart. This is handy when accumulating text across many rows. - Test with edge cases. Try empty strings,
Nullvalues, and numbers. If a variable can beNull(for example from a database),&will returnNullfor the whole expression. UseNz()(Access) or check withIf IsNull(var) Then var = ""before concatenating.
Common Mistakes
- Using + instead of &. The plus sign can add numbers if both operands look numeric, producing unexpected results. Always use
&for text. - Forgetting spaces. Concatenating
"Hello" & "World"gives "HelloWorld". Add a space literal:"Hello" & " " & "World". - Missing quotes around literal text. VBA needs double quotes around any fixed string. Without them, VBA treats the word as a variable and throws a "Variable not defined" error.
- Ignoring Null propagation. If any part is
Null, the entire concatenation becomesNull. Guard against it withIf IsNull(x) Then x = ""or use&with a default value. - Assuming & handles line breaks. To insert a line break in a cell, use
vbCrLforvbLf:"Line1" & vbCrLf & "Line2". The cell must have Wrap Text enabled to display it. - Not qualifying the worksheet. If you write
Range("A1")without specifying the sheet, VBA uses the active sheet, which may not be the one you expect. UseWorksheets("Sheet1").Range("A1").
Tips
- Use a helper function for readability. If you concatenate many fields, consider a small function that joins an array with a delimiter. VBA has
Join()for arrays:Join(Array("a","b","c"), ", ")returns "a, b, c". - Prefer & over + even for numbers. If you need to concatenate a number as text, wrap it in
CStr()orFormat()to make your intent clear. - Watch string length. VBA strings can be very long, but extremely large concatenations in loops can slow down. For building large text, consider using a
StringBuilderpattern with an array andJoinat the end. - Use
vbNullStringfor an empty string. It is slightly more efficient than""and makes your code's intent clear. - Comment complex concatenations. A long line of ampersands can be hard to read; break it across lines with the line continuation character
_(space underscore).
Troubleshooting
- "Compile error: Expected: expression". Usually a missing ampersand or a stray quote. Check that every literal string has matching double quotes and that operators are between operands.
- Result is blank or shows "Null". One of your source values is
Null. UseIsNull()to test and replace with an empty string before concatenating. - Numbers appear with unexpected decimals or thousands separators. VBA uses your system locale. Use
Format()with an explicit format string to control the output. - Line breaks do not show in the cell. Enable Wrap Text on the cell, or use
vbCrLfand setRange("A1").WrapText = True. - Macro runs on the wrong sheet. Qualify every
RangeorCellsreference with a worksheet object, e.g.,ThisWorkbook.Worksheets("Data").Range("A1"). - "Type mismatch" error. You may be trying to concatenate an object or an array without converting it. Use
CStr()on simple values, and loop through arrays to join elements.
Frequently Asked Questions
Can I use the & operator with numbers?
Yes, VBA converts numbers to text automatically, but the result depends on your regional settings. Use Format() or CStr() to control the conversion.
What is the difference between & and + in VBA?
The & operator always performs string concatenation. The + operator can perform addition if both operands are numeric, leading to bugs. Use & for all text joining.
How do I add a new line in a concatenated string?
Use vbCrLf (carriage return + line feed) or vbLf (line feed) between parts: "First" & vbCrLf & "Second". The cell must have Wrap Text enabled to display the break.
Why does my concatenation return Null?
If any part of the expression is Null, the entire result is Null. Check each variable with IsNull() and replace Nulls with empty strings before concatenating.
Can I concatenate a range of cells?
VBA does not have a built-in function to concatenate a range directly. You can loop through the cells and build the string, or use the Join() function on an array populated from the range.
Is there a limit to string length in VBA?
VBA strings can hold up to approximately 2 billion characters, but practical limits depend on available memory. For very large strings, build them efficiently (e.g., using an array and Join) to avoid performance issues.