VBA & Macro Problems

Create a VBA Function to Combine Excel Cell Text

Emma BrownEmma Brown Dec 2, 2026 868 views

Question details

Create a custom VBA function that combines text from an Excel range with a chosen separator.

Create a VBA Function to Combine Excel Cell Text
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, select Developer > Visual Basic, or press Alt+F11 on Windows.

2
Insert the function

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

3
Call it from a worksheet

Return to the worksheet and enter =CombineText(B1:F1, ", ") in a cell. Change the range or separator as needed.

Create a VBA function using Excel TEXTJOIN
Macro permissions: The function runs only when macros are enabled for the workbook. If TEXTJOIN is unavailable in your Excel version, use the cell-loop solution instead.
Free Microsoft Office alternative

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.

Work with cell ranges and text in WPS Spreadsheets.Open and edit Microsoft Excel formats such as .xlsx and .xls.Use a familiar interface for an easier transition from Excel.Access a free, lightweight office suite.
microsoft office alternative - wps office

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.