
Tips.Net > ExcelTips Home > Editing > Concatenating Ranges of Cells
Summary: The ampersand (&) operator and the CONCATENATE function are great for combining the contents of a limited number of cells. If you want to combine the contents of a range of cells, you must resort to a user-defined function, as described in this tip. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
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 Concat(myRange As Range, Optional myDelimiter As String)
Dim r As Range
Application.Volatile
For Each r In myRange
Concat = Concat & r & myDelimiter
Next r
If Len(myDelimiter) > 0 Then
Concat = Left(Concat, Len(Concat) - 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 CONCAT 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:
=CONCAT(C8:E10) =CONCAT(C8:E10,"|")
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 Concat(myRange As Range, Optional myDelimiter As String)
Dim r As Range
Application.Volatile
For Each r In myRange
If Len(r.Text) > 0 Then
Concat = Concat & r & myDelimiter
End If
Next r
If Len(myDelimiter) > 0 Then
Concat = Left(Concat, Len(Concat) - Len(myDelimiter))
End If
End Function
Tip #3062 applies to Microsoft Excel versions: 97 2000 2002 2003
Got the Time? If you work with either times or dates in Excel, you really need ExcelTips: Times and Dates. Everything you need to know about slicing, dicing, and generally working with times and dates.
No, not that type of date. If you need to do any types of work with calendar dates, Excel has the tools you need. Learn how to use those tools the easy way. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site