Loading

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.

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.

Learn more about Allen...

ExcelTips FAQ

ExcelTips Resources

Ask an Excel Question

Make a Comment

Free Business Forms

Free Calendars

** 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),

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!

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

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

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.

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

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

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

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.

Concat1 = Concat

Add that and the very first code works.

@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

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

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

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

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

Thx

Nuha

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.

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

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

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

- 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