Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Concatenating Ranges of Cells

Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Concatenating Ranges of Cells.

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

    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 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:

=CONCAT1(C8:E10)
=CONCAT1(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 Concat2(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

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3062) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Concatenating Ranges of Cells.

Related Tips:

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

strChrisP    01 Mar 2016, 18:40
Thanks for a wonderfully useful piece of code.

One other (somewhat trivial) error to correct: If the range selected is currently empty and a delimiter is specified, then the block below produces an error:
    If Len(myDelimiter) > 0 Then
        Concat = Left(Concat, Len(Concat) - Len(myDelimiter))
    End If

This needs to be surrounded thus:

If Len(Concat) > 0 Then
If Len(myDelimiter) > 0 Then
        Concat = Left(Concat, Len(Concat) - Len(myDelimiter))
    End If
End If
Alex    21 Feb 2016, 00:41
In Excel 2003 I have the following formula in cell A1 to combine text in the mentioned cells with comma separators between them. Notice that there are 2 ranges of cells.

=(A86&","A87&","&A88&","A89&","&A90&","

&A193&","&A194&","&A195&","&A196&","&A197&","&A198)

How can I use your function to achieve this? I mean, where can I put your code? and what to type in cell A1?
Than you.
Vikas Gautam    24 Dec 2014, 11:48
What about this one.
Syntax
=JoinC( Range _ Array, Dilimiter as String )
=JoinR( Range _ Array, Dilimiter as String )
For Col ConCatenation
Select Code copy to clipboard
Function JoinC(ByRef x As Variant, ByRef Delim As String) As String
'******* Developed by Vikas Gautam *******
'*****Valued Contributor at ExcelForum.com*****
'****For One dimensional Arrays having one Row and many Columns*****

With Application
    SourceArray = .Transpose(x)
    Temp = Replace(Delim & Join(SourceArray, Delim) & Delim, Delim & Delim, Delim)
    
    Do While InStr(1, Temp, Delim & Delim, 1) > 0
    Temp = Replace(Temp, Delim & Delim, Delim)
    Loop

End With
If Right(Temp, 1) = Delim And Left(Temp, 1) = Delim Then _
    JoinC = Mid(Temp, 2, Len(Temp) - 2)
   ElseIf Left(Temp, 1) = Delim Then
    JoinC = Mid(Temp, 2, Len(Temp))
   ElseIf Right(Temp, 1) = Delim Then
    JoinC = Mid(Temp, 1, Len(Temp) - 1)
End If
End Function
For Row Concatenation
Select Code copy to clipboard
Function JoinR(ByRef x As Variant, ByRef Delim As String) As String
'******* Developed by Vikas Gautam *******
'*****Valued Contributor at ExcelForum.com*****
'****For One dimensional Arrays having one Row and many Columns*****
With Application
    SourceArray = .Transpose(.Transpose(x))
    Temp = Replace(Delim & Join(SourceArray, Delim) & Delim, Delim & Delim, Delim)
    
    Do While InStr(1, Temp, Delim & Delim, 1) > 0
    Temp = Replace(Temp, Delim & Delim, Delim)
    Loop
End With
If Right(Temp, 1) = Delim And Left(Temp, 1) = Delim Then _
    JoinR = Mid(Temp, 2, Len(Temp) - 2)
   ElseIf Left(Temp, 1) = Delim Then
    JoinR = Mid(Temp, 2, Len(Temp))
   ElseIf Right(Temp, 1) = Delim Then
    JoinR = Mid(Temp, 1, Len(Temp) - 1)
End If
End Function
Aaron A. Anderson    26 Sep 2014, 16:48
Actually his very first code was correct except for one line. Right after the 'end if' and before the end function he needs to define the output.

Concat1 = Concat

Add that and the very first code works.
Michael (Micky) Avidan    19 Mar 2014, 18:49
@Duane,
Believe me - a small credit to Dave would not hurt...
http://excelribbon.tips.net/T011247_Concatenating_Ranges_of_Cells.html
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL
Duane Hookom    18 Mar 2014, 11:58
The code has several errors. Replace it with
Public Function Concat1(myRange As Range, Optional myDelimiter As String)
    Dim r As Range
    Dim Concat As String
    Application.Volatile
    For Each r In myRange
        Concat = Concat & r & myDelimiter
    Next r
    If Len(myDelimiter) > 0 Then
        Concat1 = Left(Concat, Len(Concat) - Len(myDelimiter))
    End If
End Function
NUha    17 Mar 2014, 09:08
I have added the first function concat 1 but i am always getting a zero. I am using for ex concat1(A2:A15). Is there anything that i need to add?

Thx
Nuha
Jeremy Stein    05 Feb 2013, 16:00
I think r.Text should be r.Value. I got errors when my range referenced something that was calculated from external data. The function would be invalid when the data was refreshed. Changing it to r.Value fixed my problem.
xypho    10 Jan 2013, 22:25
Thanks for that Allen, Duane, great ideas...

In fact you do not need to declare a superfluous variable (Concat)...
in addition, you could have an optional parameter to include the empty text.
I also like to leave the delimiter empty, but to each their own.
And I prefer using mid rather than left for the removal of superfluous delimiter (which means the delimiter is at the start of the string), one less string operation.


Option Explicit

Public Function Concat(myRange As Range, Optional myDelimiter As String = "", Optional includeEmptyText As Boolean = True)
    Dim r As Range
    
    'make sure this function is recalculated when the target range is recalculated
    Application.Volatile
    
    'build the concat string
    For Each r In myRange
        If (includeEmptyText Or Len(r.Text) > 0) Then
            Concat = Concat & myDelimiter & r
        End If
    Next r
    
    'remove the superfluous delimiter
    If Len(myDelimiter) > 0 Then
        Concat = Mid(Concat, Len(myDelimiter) + 1)
    End If
End Function




Duane Hookom    29 Jan 2012, 11:08
There are a couple issues with Concat2().
- concat is not declared which would have been discovered if the module General Declarations included Option Explicit
- Concat2 is not set to the value of concat so the function returns nothing.

I would also set an optional delimiter of ", " since this is the most common.

Function Concat2(myRange As Range, _
        Optional myDelimiter As String = ", ")
    Dim r As Range
    Dim concat As String
    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
    Concat2 = concat
End Function
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.