The quickest way to join a range of cells in VBA is to read the range into a Variant array and then use the Join function with Application.Transpose (for a single column or row). For example, Join(Application.Transpose(Range("A1:A10")), ", ") returns a string of the values in A1:A10 separated by commas. This method is fast, avoids looping, and works for both columns and rows with a small adjustment.
Why Not Just Use Join on a Range?
The Join function in VBA expects an array of strings, not a Range object. Passing a Range directly causes a type mismatch error. The trick is to convert the range into an array. When you assign a range to a Variant variable, VBA creates a 2D array (even for a single column or row). The Join function, however, requires a 1D array. That’s where Application.Transpose comes in: it converts the 2D array into a 1D array for a single column or row.
How to Join a Single Column or Row
Here’s a concrete example. Assume you have values in A1:A5 (e.g., 10, 20, 30, 40, 50). The following macro joins them with a comma and space:
Sub JoinColumn()
Dim rng As Range
Dim arr As Variant
Dim result As String
Set rng = Range("A1:A5")
arr = Application.Transpose(rng) ' 1D array
result = Join(arr, ", ")
Debug.Print result ' Output: 10, 20, 30, 40, 50
End Sub
For a single row (e.g., A1:C1), use a double transpose: arr = Application.Transpose(Application.Transpose(rng)). The first transpose converts the row to a column, the second to a 1D array.
Joining a Multi-Column or Multi-Row Range
If you need to join all cells in a 2D range (say A1:C3), Transpose alone won’t work because it only handles a single row or column. The standard approach is to loop through the range and build the string manually. While slower for huge ranges, it’s simple and reliable:
Sub Join2DRange()
Dim rng As Range
Dim cell As Range
Dim result As String
Set rng = Range("A1:C3")
For Each cell In rng
result = result & cell.Value & ", "
Next cell
' Remove trailing comma and space
If Len(result) > 0 Then result = Left(result, Len(result) - 2)
Debug.Print result
End Sub
This joins all cells row by row (A1, B1, C1, A2, …). If you want column by column, you can use nested loops with the Areas property or use a different array approach.
Choosing a Separator and Handling Empty Cells
The separator can be any string: comma, space, newline (vbCrLf), or a custom delimiter. To skip empty cells, you can filter the array before joining. One way is to use a loop to build a new array of non-empty values. For example:
Sub JoinNonEmpty()
Dim rng As Range
Dim cell As Range
Dim temp() As String
Dim count As Long
Dim i As Long
Set rng = Range("A1:A10")
ReDim temp(1 To rng.Cells.Count)
count = 0
For Each cell In rng
If cell.Value <> "" Then
count = count + 1
temp(count) = CStr(cell.Value)
End If
Next cell
If count > 0 Then
ReDim Preserve temp(1 To count)
Debug.Print Join(temp, " | ")
End If
End Sub
This avoids extra separators when cells are blank.
Common Mistakes and How to Avoid Them
- Type mismatch: Forgetting to transpose a single column or row. Always use
Application.Transposefor one-dimensional ranges. - Using Join on a 2D array:
Joinonly works with a 1D array. For multi-column ranges, loop or flatten the array first. - Losing values with Transpose: If your range has more than 65,536 cells,
Application.Transposefails (Excel’s limit). For large ranges, loop or use a different method. - Not converting to string: If your cells contain numbers,
Joinautomatically converts them to strings, but if you have errors (like #N/A), you’ll get an error string. Handle errors explicitly if needed.
Performance: Looping vs. Array
For small ranges (a few hundred cells), looping is fine. For thousands of cells, using an array with Join is dramatically faster because it avoids repeated cell access. The transposed array method is the best for single column/row. For 2D ranges, you can still use an array by reading the range into a Variant and then flattening it with a nested loop, but that’s essentially the same as looping through cells. In practice, the array method is preferred for speed.
Frequently Asked Questions
Can I join a range with a line break between each value?
Yes, use vbCrLf as the delimiter: Join(arr, vbCrLf). For a column, this creates a list with each value on a new line. This is useful for creating readable text or for pasting into a text file.
How do I join only visible cells (after filtering)?
Use the Range.SpecialCells(xlCellTypeVisible) to get only visible cells, then loop through that range. However, SpecialCells returns a Range that may not be contiguous, so you can’t directly assign to an array. Loop through the visible cells and build a string.
What if my range contains formulas? Does Join return the formula or the value?
It returns the value of the cell (the result of the formula), not the formula text. If you need the formula, use cell.Formula instead of cell.Value in your loop.
Is there a way to join a range without using Transpose?
Yes, you can loop through the range and concatenate, or use the WorksheetFunction.TextJoin function (available in Excel 2016+). In VBA, you can call WorksheetFunction.TextJoin(", ", True, rng) which ignores empty cells and works on 2D ranges directly. This is often simpler than the array method, though it may be slightly slower.
Can I join a range into a single cell using a formula instead of VBA?
Yes, use the TEXTJOIN function in Excel: =TEXTJOIN(", ", TRUE, A1:A10). This does the same thing without VBA. In VBA, you can also assign that formula to a cell, but if you need the result as a string variable, the VBA methods above are more direct.
Alternatives to VBA Join
If you’re not tied to VBA, consider using the TEXTJOIN worksheet function, which handles 2D ranges and ignores empty cells. For quick one-off tasks, you can also copy the range into a text editor using paste special, but that’s manual. In VBA, the Join function is the cleanest for arrays, but for ranges, the WorksheetFunction.TextJoin is a robust alternative that avoids the transpose complexity.
Related
- Casio Calculator Algebra: Step-by-Step Guide
- Battery Calculation: How to Size Any Battery Bank
- Google Sheets App Script For Loop: How to Use
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Array Indexing: Bounds, Loops, and ExamplesAn Excel VBA array index is the number used to identify an element in an array. In VBA,…
- Excel VBA: Find the Last Row With Data in a ColumnIn Excel VBA, the usual way to find the last occupied row in one column is to start…
- VBA MsgBox Cancel: How to Exit a Sub SafelyTo stop a VBA Sub when someone clicks Cancel, use a MsgBox that includes the Cancel button, check…
- Add a Hyperlink to a VBA MsgBox in ExcelYou cannot add a genuinely clickable hyperlink inside Excel VBA’s built-in MsgBox: it displays plain text and its…
- Excel VBA Expected Expression Error: Causes and FixesIn Excel VBA, Expected: expression is a compile error: VBA reached a place where it needs a value…
- What Is a Macro Cell Tower? Definition & How It WorksA macro cell tower is the tall, freestanding structure that provides the broadest wireless coverage in a cellular…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.