Smart Systems for Home

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

Excel VBA Join: Combine Text from an Array

· 7 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

Excel VBA's Join function combines every item in a one-dimensional array into a single text string, separated by a delimiter you choose. The syntax is Join(sourceArray, [delimiter], [start], [count]). Only sourceArray is required; leave out delimiter and VBA uses a space, and leave out start and count and the whole array is used. It does the same job as TEXTJOIN does on the worksheet, but inside your macro, which means you can build export strings, file paths, email lists and SQL fragments without touching any cells.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer; on Mac, Excel > Preferences > Ribbon & Toolbar). Labels differ slightly by version, so wording you see may not match exactly.
  • A source array to join. Join only accepts a one-dimensional array of strings. A Variant array of variants usually works because VBA coerces each value to text, but a range read as a two-dimensional array will fail.
  • A destination: a message box for testing, or a cell, variable or text file for real output.
  • Saved work before you run anything. Macros that write to the sheet and fail halfway can leave partial data behind.

Step-by-Step

  1. Open the Visual Basic Editor. Press Alt+F11 on Windows, or Option+F11 on a Mac. In the Project pane, right-click your workbook and choose Insert > Module. Keeping the code in a standard module makes it easy to call from anywhere.
  2. Write a bare-bones procedure. Start with a Sub, declare a string variable, and join a simple array to see the output shape:
    Sub JoinDemo()
        Dim parts As Variant
        Dim result As String
        parts = Array("Anna", "Ben", "Cara")
        result = Join(parts, ", ")
        MsgBox result
    End Sub
    Press F5. You should see Anna, Ben, Cara.
  3. Pick your delimiter deliberately. A comma-space is right for readable lists, a comma alone for CSV, a semicolon for locales or systems where commas are decimal separators, and a vertical bar or newline for data you will split again later. Join does not escape delimiters, so if a value itself contains a comma, you will not be able to split the string reliably afterwards. Clean or wrap those values first.
  4. Feed it a real array of strings. The safest source is a String array you fill yourself:
    Dim names(1 To 3) As String
    names(1) = "Sales"
    names(2) = "Marketing"
    names(3) = "Support"
    Debug.Print Join(names, " | ")
    Note that this array is indexed from 1. That matters for the next step.
  5. Use start and count when you only want part of the array. The third argument is the index of the first element to include and the fourth is how many to include. Join(names, ", ", 2, 2) returns Marketing, Support. Be careful: start refers to the array's own lower bound, not to the first non-empty item. If you built the array with Array(), the lower bound depends on the module's Option Base setting, which is usually 0.
  6. Join from a worksheet range correctly. A range is two-dimensional even when it is one column, so pass it through a one-dimensional array first. The worksheet function Transpose flips a column into a row, which Join accepts:
    Dim vals As Variant
    Dim out As String
    vals = Application.Transpose(Range("A1:A10").Value)
    out = Join(vals, "; ")
    For a row, transpose twice. For data that may be empty or very long, Transpose has limits, so loop and build the string manually if the range is large.
  7. Skip blank cells. Join happily inserts delimiters for empty strings, giving you a, , b. Build a cleaned array by looping, or use a helper that only adds non-empty values:
    Dim cell As Range
    Dim buf As String
    For Each cell In Range("A1:A10")
        If Len(Trim(cell.Value)) > 0 Then
            buf = buf & IIf(Len(buf) > 0, ", ", "") & cell.Value
        End If
    Next cell
  8. Handle anything that is not already text. Dates and numbers are converted using your regional settings, so a date may come out as 04/12/2026 or 12/04/2026 depending on the machine. Format them with Format(value, "yyyy-mm-dd") before joining if the string leaves Excel. Currency and decimals have the same problem.
  9. Send the result somewhere useful. Write it to a cell with Range("C1").Value = result, print it with Debug.Print while testing, or push it into a text file with the FileSystemObject or Open ... For Output. Never write to the sheet inside a loop over many rows; build the string, then write once.
  10. Test edge cases. Run the macro with an empty array, with one item, with values containing your delimiter, and with Null values from a database query. Join on an empty array returns an empty string, but a Null element raises an error you must screen out first.

Common Mistakes

  • Passing a range directly. Join(Range("A1:A5"), ",") fails because the range is two-dimensional. Transpose first or build a loop.
  • Assuming the array starts at 1. With Array() and default Option Base, the first item is index 0. Using start = 1 silently drops your first entry.
  • Forgetting the delimiter. Omitting it gives spaces, not commas, which produces a readable but often wrong result for exports.
  • Joining Null. Database and lookup results can be Null; convert with Nz-style checks or CStr on a validated value.
  • Assuming the reverse works the same way. Split is not the exact inverse of Join when values contain delimiters, and it always returns a zero-based array of strings.
  • Ignoring length limits. A cell holds a finite amount of text, and a string built from thousands of rows can exceed it or make the macro slow.

Tips

  • Add Option Explicit at the top of the module so undeclared variables are caught before they cause silent errors.
  • Use vbCrLf as the delimiter to build multi-line text for a message box or a text file.
  • Keep a small reusable function that takes a range and returns a joined string, filtering blanks, so every macro in the workbook behaves the same.
  • Use vbTab when the string is going into a tab-separated file, and quote values containing commas when producing CSV by hand.
  • Turn off ScreenUpdating and Calculation around long loops that build strings, then turn them back on in the same procedure.
  • Use Debug.Print and the Immediate window (Ctrl+G) to inspect partial strings instead of adding temporary message boxes.

Troubleshooting

Compile error: ByRef argument type mismatch. You are passing a variable declared as something other than a string or variant array. Declare it as Variant or String() and retry.

Runtime error 13, type mismatch. Usually a two-dimensional range or a non-array variable. Check with Debug.Print IsArray(vals) and either transpose or rebuild the array.

Result has doubled or trailing delimiters. Blank or whitespace cells are being joined. Trim each value and skip empties, as in step 7.

First item missing. Your start argument is off by one. Print the lower bound with LBound(vals) and UBound(vals) to confirm where the array begins.

Dates or numbers look wrong. Format each value explicitly before joining rather than trusting regional conversion.

Macro runs but nothing appears. Check where you sent the output. If it was a message box, verify the string is not empty by printing Len(result). If it was a cell, confirm the sheet is not protected and the write is outside the loop.

If none of that helps, undo any recent edits and rebuild the procedure a few lines at a time, running after each addition, so the failing line is obvious.

Frequently Asked Questions

What is the difference between Join and Concatenate in VBA?

The & operator joins a fixed list of values you name individually. Join combines an entire array with one delimiter, so it scales when the number of items is unknown or changes.

Can Join work on a two-dimensional array?

No. Join requires a one-dimensional array. Transpose a single column or row into a one-dimensional array first, or loop through the cells.

What delimiter does Join use if I leave it out?

A single space. Always state the delimiter explicitly unless a space is genuinely what you want.

Does Join add quotes around text values?

No, it inserts values raw. If you need quoted or escaped output for CSV or SQL, add the quote characters yourself while building each element.

Is Join faster than looping with the & operator?

For large arrays, yes, because it avoids repeated string reallocation. For a handful of items the difference is negligible, but the code is cleaner either way.

Does Join exist in Excel VBA on Mac?

Yes, it is a standard VBA function available in both Windows and Mac Excel. Only the keyboard shortcut and some menu locations differ.