Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Concatenating Ranges of Cells.
Written by Allen Wyatt (last updated September 12, 2019)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Excel provides one workbook function and one operator that both have the same purpose—to combine strings into a longer string. The CONCATENATE function and the ampersand (&) operator have essentially the same purpose.
Many people use the ampersand operator in preference to the CONCATENATE function because it requires less typing, but CONCATENATE would become immensely more valuable if it would handle a range of cells. Unfortunately it does not, but you can create your own user-defined function that will concatenate every cell in a range very nicely. Consider the following macro:
Function Concat1(myRange As Range, Optional myDelimiter As String) Dim r As Range For Each r In myRange Concat1 = Concat1 & r & myDelimiter Next r If Len(myDelimiter) > 0 Then Concat1 = Left(Concat1, Len(Concat1) - Len(myDelimiter)) End If End Function
This function requires a range and provides for an optional delimiter. The last "If" statement removes the final trailing delimiter from the concatenated string. With the CONCAT1 function, cells can be added and deleted within the range, without the maintenance required by CONCATENATE or ampersand formulas. All you need to do is call the function in one of the following manners:
The second method of calling the function uses the optional delimiter, which is inserted between each of the concatenated values from the range C8:E10. There is a problem with this, however: If a cell in that range is empty, then you can end up with two sequential delimiters. If you prefer to have only a single delimiter, then you need to make one small change to the function:
Function Concat2(myRange As Range, Optional myDelimiter As String) Dim r As Range For Each r In myRange If Len(r.Text) > 0 Then Concat2 = Concat2 & r & myDelimiter End If Next r If Len(myDelimiter) > 0 Then Concat2 = Left(Concat2, Len(Concat2) - Len(myDelimiter)) End If End Function
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11247) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Concatenating Ranges of Cells.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Referring to a range of cells on the current worksheet is easy. But what if you want to refer to a range on a number of ...Discover More
How you add special characters to Excel can differ from how you add them in other Office programs, such as Word. This tip ...Discover More
Need to change some cell references in your defined names? Changing one or two is easy; changing dozens is a good deal ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.