**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**.

**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!

What happens when you press Enter in a cell depends on how you have Excel configured. Here's the way you can control the ...

Discover MoreWant a quick way to tell how may rows and columns you've selected? Here's what I do when I need to know that information.

Discover MoreGot a worksheet in which there may be entire columns that are duplicates of each other? If you want to delete those duplicate ...

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

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

Copyright © 2017 Sharon Parq Associates, Inc.

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