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.

Concatenating Ranges of Cells

by Allen Wyatt
(last updated May 21, 2015)

10

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Unhiding a Single Column

In a worksheet with lots of hidden columns it is a real pain to try to unhide just one or two columns. The best solution is ...

Discover More

Jumping to Alphabetic Worksheets

Got a workbook with a lot of worksheets in it? Here are some handy ways to jump to the worksheet you want, alphabetically.

Discover More

Jumping to a Specific Cell Using a Hyperlink

Excel allows you to define hyperlinks in your worksheets, and these can target specific cells on other worksheets. Here are ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Default Cell Movement when Deleting

Delete a cell or a range of cells, and Excel needs to figure out how to rearrange the void left by the deletion. You can ...

Discover More

Using Overtype Mode

Have you ever typed something in Excel, only to have it replace whatever is to the right of the insertion point? That's known ...

Discover More

Understanding Names

Excel provides the ability to define names that refer to cells or ranges of cells. These can then be used in your formulas to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 9 - 2?

2016-03-01 18:40:30

strChrisP

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


2016-02-21 00:41:19

Alex

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.


2014-12-24 11:48:50

Vikas Gautam

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


2014-09-26 16:48:58

Aaron A. Anderson

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.


2014-03-19 18:49:13

Michael (Micky) Avidan

@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


2014-03-18 11:58:35

Duane Hookom

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


2014-03-17 09:08:45

NUha

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


2013-02-05 16:00:28

Jeremy Stein

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.


2013-01-10 22:25:30

xypho

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





2012-01-29 11:08:00

Duane Hookom

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


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.