Create a VBA Function to Combine Excel Cell Text
Question details
Create a custom VBA function that combines text from an Excel range with a chosen separator.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user wants to call a VBA function from a worksheet to join values from several cells.
- Observed behavior
- The desired function returns the cell values as one text string, separated by the specified delimiter.
Check whether your Excel version supports TEXTJOIN. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm), and enable macros only if you trust the workbook.
Create a VBA function using Excel TEXTJOIN
Use Excel's TEXTJOIN method to combine a range while ignoring empty cells.
TEXTJOIN is an Excel function, so call it through the Application object rather than as an ordinary VBA function. This method requires an Excel version that supports TEXTJOIN.
In Excel, select Developer > Visual Basic, or press Alt+F11 on Windows.
In the VBA editor, select Insert > Module and paste: Function CombineText(str As Range, sep As String) As String CombineText = Application.TextJoin(sep, True, str) End Function
Return to the worksheet and enter =CombineText(B1:F1, ", ") in a cell. Change the range or separator as needed.

Combine cell values with a VBA loop
Use this approach when TEXTJOIN is unavailable or you prefer to process cells individually.
Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and compatibility with Excel files, making it easy to move everyday spreadsheet work. VBA support and macro behavior can vary by WPS edition and environment, so check compatibility before relying on a custom function.

Frequently Asked Questions
How do I change the separator between cell values?
Change the second argument in the worksheet formula. For example, =CombineText(B1:F1, " - ") inserts a hyphen with spaces between values.
Why does Excel show #NAME? for my custom function?
Check that CombineText is in a standard module inserted through Visual Basic > Insert > Module, that the function name is spelled correctly, and that macros are enabled for the trusted workbook.
Does the function include empty cells?
The TEXTJOIN example uses True to ignore empty cells. The loop example also skips cells whose converted value is an empty string.
What if my Excel version does not support TEXTJOIN?
Use the cell-loop implementation in a standard VBA module instead. It combines values without calling Excel's TEXTJOIN function.
Which workbook format keeps the VBA function?
Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). The regular .xlsx format cannot retain VBA macros.




