VBA & Operator: How to Use And in Excel Macros

VBA & macros · Updated November 23, 2026

The & operator in VBA is the string concatenation operator — it joins two or more text values into one single string. For example, MsgBox "Part " & 1 displays “Part 1”. It is not a logical AND (that is the And keyword); the ampersand is used exclusively for combining text, numbers, and variables into a single text result.

What the & Operator Does

In VBA, the ampersand (&) takes everything on its left and right, converts each operand to a String data type, and returns a new string. It is the recommended concatenation operator because it always treats operands as text, unlike the + operator which can add numbers or cause a type mismatch when mixing text and numbers.

Basic examples:

  • Debug.Print "Hello " & "World" → Hello World
  • Debug.Print "Total: " & 100 → Total: 100
  • Debug.Print 10 & 20 → 1020 (not 30, because & concatenates)

The & operator is essential for building dynamic messages, constructing file paths, creating SQL query strings, and formatting output in Excel macros.

How to Use & in VBA Code

You use & anywhere you need a string expression. Here is a simple macro that greets a user by name:

Sub GreetUser()
    Dim userName As String
    userName = "Alice"
    MsgBox "Hello, " & userName & "!"
End Sub

You can concatenate multiple values in one line. The following builds a file path:

Sub BuildPath()
    Dim folder As String
    Dim fileName As String
    folder = "C:\Reports\"
    fileName = "Q1.xlsx"
    Debug.Print folder & fileName  ' Output: C:\Reports\Q1.xlsx
End Sub

When concatenating numbers, VBA automatically converts them to their string representation. This is handy for creating labels like "Row " & rowNumber.

& vs + for Concatenation

Many beginners use + to join strings, but that is risky. The + operator behaves differently depending on the data types:

  • "5" + 5 → 10 (numeric addition, because both can be treated as numbers)
  • "5" & 5 → "55" (always text concatenation)
  • "Hello" + 5 → Type mismatch error (because text and number cannot be added)
  • "Hello" & 5 → "Hello5" (works fine)

Always prefer & for string building. It avoids errors and makes your intention clear.

Common Mistakes and Pitfalls

One frequent error is forgetting to include spaces in the concatenated result. For example, "Hello" & "World" gives HelloWorld. You must add spaces manually: "Hello " & "World".

Another issue is using & with Null values. In VBA, concatenating a Null with & returns Null, which can cause unexpected results. Use Nz() or an If check to handle Null.

Also, when building long strings, line breaks can be tricky. Use the line continuation character (underscore) to split code across lines:

Dim msg As String
msg = "This is a long message " & _
      "that continues on the next line."

Remember: the underscore must be preceded by a space and be the last character on the line.

Using & with Variables and Functions

You can concatenate the results of functions and property values. For instance, to show a cell address:

Sub ShowAddress()
    Dim rng As Range
    Set rng = Range("A1")
    MsgBox "The cell is " & rng.Address
End Sub

You can also combine with Format to control number formatting:

Dim price As Double
price = 19.99
MsgBox "Price: $" & Format(price, "0.00")

This outputs Price: $19.99.

Concatenation in Loops and Arrays

In loops, you often accumulate a string. The & operator is efficient for small strings, but for very large loops, repeated concatenation can be slow. In that case, use a StringBuilder-like approach with a VBA.Collection or the Join function.

Example using a loop to build a comma-separated list:

Sub BuildList()
    Dim i As Integer
    Dim result As String
    result = ""
    For i = 1 To 5
        result = result & i & ","
    Next i
    ' Remove trailing comma
    result = Left(result, Len(result) - 1)
    Debug.Print result  ' Output: 1,2,3,4,5
End Sub

For large datasets, consider using an array and Join:

Dim arr(1 To 5) As String
Dim i As Integer
For i = 1 To 5
    arr(i) = CStr(i)
Next i
Debug.Print Join(arr, ",")

Frequently Asked Questions

Is & the same as the logical AND in VBA?

No. The logical AND is the And keyword, used in conditions like If x > 5 And x < 10 Then. The ampersand (&) is only for string concatenation. Mixing them up is a common syntax error.

Can I concatenate a date with &?

Yes. VBA converts the date to its short date string based on your system’s regional settings. For example, "Date: " & Date might return Date: 3/15/2025. To control the format, use Format(Date, "yyyy-mm-dd").

Why does “5” & 5 give “55” but “5” + 5 gives 10?

Because & always treats operands as strings, so both “5” and 5 become text “5” and are joined. The + operator, when both operands look like numbers, performs arithmetic addition. If one is text and the other is a number, + raises a type mismatch error.

How do I insert a newline in a concatenated string?

Use the VBA constant vbNewLine (or vbCrLf). Example: "Line1" & vbNewLine & "Line2". For Excel, you can also use Chr(10) if you need a line feed only.

Does & work in Excel formulas?

Yes, the ampersand is also the concatenation operator in Excel formulas, like =A1&B1. In VBA, you use it in code, but you can also write formulas that use & by assigning a string: Range("C1").Formula = "=A1&B1".

Performance and Best Practices

For most macros, & is fast enough. But if you are concatenating thousands of strings in a loop, the repeated memory allocation can slow things down. Use Join with an array for large-scale concatenation.

Always declare your variables with explicit types to avoid implicit conversions. When concatenating numbers, you can use CStr() to be explicit, though it is not required.

Finally, remember that macros cannot be undone with Ctrl+Z. Always save a copy of your workbook before running a macro that modifies data, especially if it uses concatenation to overwrite cell values.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.